001/**
002 * #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
003 *   This file is part of the LDP4j Project:
004 *     http://www.ldp4j.org/
005 *
006 *   Center for Open Middleware
007 *     http://www.centeropenmiddleware.com/
008 * #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
009 *   Copyright (C) 2014 Center for Open Middleware.
010 * #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
011 *   Licensed under the Apache License, Version 2.0 (the "License");
012 *   you may not use this file except in compliance with the License.
013 *   You may obtain a copy of the License at
014 *
015 *             http://www.apache.org/licenses/LICENSE-2.0
016 *
017 *   Unless required by applicable law or agreed to in writing, software
018 *   distributed under the License is distributed on an "AS IS" BASIS,
019 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020 *   See the License for the specific language governing permissions and
021 *   limitations under the License.
022 * #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
023 *   Artifact    : org.ldp4j.framework:ldp4j-application-api:0.2.0
024 *   Bundle      : ldp4j-application-api-0.2.0.jar
025 * #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
026 */
027package org.ldp4j.application.data;
028
029import static com.google.common.base.Preconditions.checkNotNull;
030
031import java.util.Calendar;
032import java.util.Date;
033import java.util.GregorianCalendar;
034import java.util.Locale;
035import java.util.TimeZone;
036import java.util.concurrent.TimeUnit;
037
038import javax.xml.datatype.DatatypeConfigurationException;
039import javax.xml.datatype.DatatypeFactory;
040import javax.xml.datatype.XMLGregorianCalendar;
041
042import org.joda.time.DateTime;
043import org.joda.time.Duration;
044
045public final class TimeUtils {
046
047  public final class DateTimeConverter {
048
049    private final DateTime literal;
050
051    private DateTimeConverter(DateTime literal) {
052      this.literal = literal;
053    }
054
055    public long to(TimeUnit unit) {
056      return unit.convert(toDate().getTime(),TimeUnit.MILLISECONDS);
057    }
058
059    public Date toDate() {
060      return this.literal.toDate();
061    }
062
063    public java.sql.Date toSqlDate() {
064      return new java.sql.Date(toDate().getTime());
065    }
066
067    public java.sql.Time toSqlTime() {
068      return new java.sql.Time(toDate().getTime());
069    }
070
071    public Calendar toCalendar() {
072      return this.literal.toCalendar(TimeUtils.this.locale);
073    }
074
075    public GregorianCalendar toGregorianCalendar() {
076      return this.literal.toGregorianCalendar();
077    }
078
079    public XMLGregorianCalendar toXMLGregorianCalendar() {
080      return TimeUtils.this.datatypeFactory.newXMLGregorianCalendar(toGregorianCalendar());
081    }
082
083  }
084
085  public final class DurationConverter {
086
087    private final Duration duration;
088
089    private DurationConverter(Duration literal) {
090      this.duration=literal;
091    }
092
093    public long millis() {
094      return this.duration.getMillis();
095    }
096
097    public long to(TimeUnit unit) {
098      return unit.convert(millis(),TimeUnit.MILLISECONDS);
099    }
100
101    public javax.xml.datatype.Duration toDuration() {
102      return TimeUtils.this.datatypeFactory.newDuration(millis());
103    }
104
105  }
106
107  private final TimeZone timezone;
108  private final Locale locale;
109  private final XMLGregorianCalendar defaults;
110  private final DatatypeFactory datatypeFactory;
111
112  private TimeUtils(DatatypeFactory datatypeFactory, TimeZone timezone, Locale locale, XMLGregorianCalendar defaults) {
113    this.datatypeFactory = datatypeFactory;
114    this.timezone = timezone;
115    this.locale = locale;
116    this.defaults = defaults;
117  }
118
119  public TimeUtils withTimeZone(TimeZone timezone) {
120    return new TimeUtils(this.datatypeFactory,timezone,this.locale,this.defaults);
121  }
122
123  public TimeUtils withLocale(Locale locale) {
124    return new TimeUtils(this.datatypeFactory,this.timezone,locale,this.defaults);
125  }
126
127  public TimeUtils withDefaults(XMLGregorianCalendar defaults) {
128    return new TimeUtils(this.datatypeFactory,this.timezone,this.locale,defaults);
129  }
130
131  public DateTimeConverter from(DateTime literal) {
132    checkNotNull(literal,"Literal cannot be null");
133    return new DateTimeConverter(literal);
134  }
135
136  public DurationConverter from(Duration literal) {
137    checkNotNull(literal,"Literal cannot be null");
138    return new DurationConverter(literal);
139  }
140
141  public static TimeUtils newInstance() {
142    try {
143      return new TimeUtils(
144        DatatypeFactory.newInstance(),
145        TimeZone.getDefault(),
146        Locale.getDefault(),
147        null);
148    } catch (DatatypeConfigurationException e) {
149      throw new AssertionError("Could not create factory",e);
150    }
151  }
152
153}