org.apache.asterix.om.base.ADuration Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.asterix.om.base;
import org.json.JSONException;
import org.json.JSONObject;
import org.apache.asterix.common.exceptions.AsterixException;
import org.apache.asterix.om.base.temporal.GregorianCalendarSystem;
import org.apache.asterix.om.types.BuiltinType;
import org.apache.asterix.om.types.IAType;
import org.apache.asterix.om.visitors.IOMVisitor;
/**
* ADuration type represents time duration (unanchored time length) values.
*
* An ADuration value may contain the same fields as the {@link ADateTime}:
* - year;
* - month;
* - day;
* - hour;
* - minute;
* - second;
* - millisecond.
* Compared with {@link ADateTime}, a field in a duration value does not have the limited domain requirement. A duration field is valid as far as the value of the field is an integer.
*
* We also support negative durations, which is not specified by ISO 8601, but has been
* supported by XML spec to enable the arithmetic operations between time instances.
*
* Internally, an ADuration value is stored as two fields: an integer field as the number of months for the YEAR and MONTH fields, and a long integer field as the number of milliseconds for the other fields.
*
*/
public class ADuration implements IAObject {
/**
* number of full months represented by the year-month part
*/
protected int chrononInMonth;
/**
* number of milliseconds represented by the part other than the year and month
*/
protected long chrononInMillisecond;
public ADuration(int months, long seconds) {
this.chrononInMonth = months;
this.chrononInMillisecond = seconds;
}
public int getMonths() {
return chrononInMonth;
}
public long getMilliseconds() {
return chrononInMillisecond;
}
@Override
public IAType getType() {
return BuiltinType.ADURATION;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof ADuration)) {
return false;
} else {
ADuration d = (ADuration) o;
return d.getMonths() == chrononInMonth && d.getMilliseconds() == chrononInMillisecond;
}
}
@Override
public int hashCode() {
return (int) (chrononInMonth ^ (chrononInMillisecond) ^ (chrononInMillisecond >>> 32));
}
@Override
public void accept(IOMVisitor visitor) throws AsterixException {
visitor.visitADuration(this);
}
@Override
public boolean deepEqual(IAObject obj) {
return equals(obj);
}
@Override
public int hash() {
return hashCode();
}
@Override
public String toString() {
StringBuilder sbder = new StringBuilder();
sbder.append("ADuration: {");
GregorianCalendarSystem.getInstance().getDurationExtendStringRepWithTimezoneUntilField(chrononInMillisecond,
chrononInMonth, sbder);
sbder.append(" }");
return sbder.toString();
}
@Override
public JSONObject toJSON() throws JSONException {
JSONObject json = new JSONObject();
JSONObject duration = new JSONObject();
duration.put("months", chrononInMonth);
duration.put("milliseconds", chrononInMillisecond);
json.put("ADuration", duration);
return json;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy