All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jooq.types.YearToMonth Maven / Gradle / Ivy

There is a newer version: 3.19.11
Show newest version
/**
 * Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com)
 * All rights reserved.
 *
 * Licensed 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.
 *
 * Other licenses:
 * -----------------------------------------------------------------------------
 * Commercial licenses for this work are available. These replace the above
 * ASL 2.0 and offer limited warranties, support, maintenance, and commercial
 * database integrations.
 *
 * For more information, please visit: http://www.jooq.org/licenses
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */
package org.jooq.types;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jooq.Field;
import org.jooq.SQLDialect;

/**
 * An implementation for the SQL standard INTERVAL YEAR TO MONTH
 * data type.
 * 

* YearToMonth is a {@link Number} whose {@link Number#intValue()} * represents the number of months of the interval. *

* Note: only a few databases actually support this data type on its own. You * can still use it for date time arithmetic in other databases, though, through * {@link Field#add(Field)} and {@link Field#sub(Field)} Databases that have * been observed to natively support INTERVAL data types are: *

    *
  • {@link SQLDialect#HSQLDB}
  • *
  • {@link SQLDialect#INGRES}
  • *
  • {@link SQLDialect#ORACLE}
  • *
  • {@link SQLDialect#POSTGRES}
  • *
*

* These dialects have been observed to partially support INTERVAL * data types in date time arithmetic functions, such as * TIMESTAMPADD, and TIMESTAMPDIFF: *

    *
  • {@link SQLDialect#CUBRID}
  • *
  • {@link SQLDialect#MARIADB}
  • *
  • {@link SQLDialect#MYSQL}
  • *
* * @author Lukas Eder * @see Interval */ public final class YearToMonth extends Number implements Interval, Comparable { /** * Generated UID */ private static final long serialVersionUID = 1308553645456594273L; private static final Pattern PATTERN = Pattern.compile("(\\+|-)?(\\d+)-(\\d+)"); private final boolean negative; private final int years; private final int months; /** * Create a new year-month interval. */ public YearToMonth(int years) { this(years, 0, false); } /** * Create a new year-month interval. */ public YearToMonth(int years, int months) { this(years, months, false); } private YearToMonth(int years, int months, boolean negative) { // Perform normalisation. Specifically, Postgres may return intervals // such as 0-13 if (months >= 12) { years += (months / 12); months %= 12; } this.negative = negative; this.years = years; this.months = months; } /** * Parse a string representation of a INTERVAL YEAR TO MONTH * * @param string A string representation of the form * [+|-][years]-[months] * @return The parsed YEAR TO MONTH object, or * null if the string could not be parsed. */ public static YearToMonth valueOf(String string) { if (string != null) { Matcher matcher = PATTERN.matcher(string); if (matcher.find()) { boolean negative = "-".equals(matcher.group(1)); int years = Integer.parseInt(matcher.group(2)); int months = Integer.parseInt(matcher.group(3)); return new YearToMonth(years, months, negative); } } return null; } // ------------------------------------------------------------------------- // XXX Interval API // ------------------------------------------------------------------------- @Override public final YearToMonth neg() { return new YearToMonth(years, months, !negative); } @Override public final YearToMonth abs() { return new YearToMonth(years, months, false); } public final int getYears() { return years; } public final int getMonths() { return months; } @Override public final int getSign() { return negative ? -1 : 1; } // ------------------------------------------------------------------------- // XXX Number API // ------------------------------------------------------------------------- @Override public final int intValue() { return (negative ? -1 : 1) * (12 * years + months); } @Override public final long longValue() { return intValue(); } @Override public final float floatValue() { return intValue(); } @Override public final double doubleValue() { return intValue(); } // ------------------------------------------------------------------------- // XXX Comparable and Object API // ------------------------------------------------------------------------- @Override public final int compareTo(YearToMonth that) { if (years < that.years) { return -1; } if (years > that.years) { return 1; } if (months < that.months) { return -1; } if (months > that.months) { return 1; } return 0; } @Override public final int hashCode() { final int prime = 31; int result = 1; result = prime * result + months; result = prime * result + years; return result; } @Override public final boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; YearToMonth other = (YearToMonth) obj; if (months != other.months) return false; if (years != other.years) return false; return true; } @Override public final String toString() { StringBuilder sb = new StringBuilder(); sb.append(negative ? "-" : "+"); sb.append(years); sb.append("-"); sb.append(months); return sb.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy