1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $PostgreSQL: pgjdbc/org/postgresql/util/PGInterval.java,v 1.14 2008/01/08 06:56:31 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.util;
import java.io.Serializable;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Calendar;
import java.util.Date;
import java.util.StringTokenizer;
/**
* This implements a class that handles the PostgreSQL interval type
*/
public class PGInterval extends PGobject implements Serializable, Cloneable
{
private int years;
private int months;
private int days;
private int hours;
private int minutes;
private double seconds;
private final static DecimalFormat secondsFormat;
static {
secondsFormat = new DecimalFormat("0.00####");
DecimalFormatSymbols dfs = secondsFormat.getDecimalFormatSymbols();
dfs.setDecimalSeparator('.');
secondsFormat.setDecimalFormatSymbols(dfs);
}
/**
* required by the driver
*/
public PGInterval()
{
setType("interval");
}
/**
* Initialize a interval with a given interval string representation
*
* @param value String representated interval (e.g. '3 years 2 mons')
* @throws SQLException Is thrown if the string representation has an unknown format
* @see #setValue(String)
*/
public PGInterval(String value)
throws SQLException
{
this();
setValue(value);
}
/**
* Initializes all values of this interval to the specified values
*
* @see #setValue(int, int, int, int, int, double)
*/
public PGInterval(int years, int months, int days, int hours, int minutes, double seconds)
{
this();
setValue(years, months, days, hours, minutes, seconds);
}
/**
* Sets a interval string represented value to this instance.
* This method only recognize the format, that Postgres returns -
* not all input formats are supported (e.g. '1 yr 2 m 3 s')!
*
* @param value String representated interval (e.g. '3 years 2 mons')
* @throws SQLException Is thrown if the string representation has an unknown format
*/
public void setValue(String value)
throws SQLException
{
final boolean ISOFormat = !value.startsWith("@");
// Just a simple '0'
if (!ISOFormat && value.length() == 3 && value.charAt(2) == '0')
{
setValue(0, 0, 0, 0, 0, 0.0);
return;
}
int years = 0;
int months = 0;
int days = 0;
int hours = 0;
int minutes = 0;
double seconds = 0;
try
{
String valueToken = null;
value = value.replace('+', ' ').replace('@', ' ');
final StringTokenizer st = new StringTokenizer(value);
for (int i = 1; st.hasMoreTokens(); i++)
{
String token = st.nextToken();
if ((i & 1) == 1)
{
int endHours = token.indexOf(':');
if (endHours == -1)
{
valueToken = token;
continue;
}
// This handles hours, minutes, seconds and microseconds for
// ISO intervals
int offset = (token.charAt(0) == '-') ? 1 : 0;
hours = nullSafeIntGet(token.substring(offset+0, endHours));
minutes = nullSafeIntGet(token.substring(endHours+1, endHours+3));
// Pre 7.4 servers do not put second information into the results
// unless it is non-zero.
int endMinutes = token.indexOf(':', endHours+1);
if (endMinutes != -1)
seconds = nullSafeDoubleGet(token.substring(endMinutes+1));
if (offset == 1)
{
hours = -hours;
minutes = -minutes;
seconds = -seconds;
}
valueToken = null;
}
else
{
// This handles years, months, days for both, ISO and
// Non-ISO intervals. Hours, minutes, seconds and microseconds
// are handled for Non-ISO intervals here.
if (token.startsWith("year"))
years = nullSafeIntGet(valueToken);
else if (token.startsWith("mon"))
months = nullSafeIntGet(valueToken);
else if (token.startsWith("day"))
days = nullSafeIntGet(valueToken);
else if (token.startsWith("hour"))
hours = nullSafeIntGet(valueToken);
else if (token.startsWith("min"))
minutes = nullSafeIntGet(valueToken);
else if (token.startsWith("sec"))
seconds = nullSafeDoubleGet(valueToken);
}
}
}
catch (NumberFormatException e)
{
throw new PSQLException(GT.tr("Conversion of interval failed"), PSQLState.NUMERIC_CONSTANT_OUT_OF_RANGE, e);
}
if (!ISOFormat && value.endsWith("ago"))
{
// Inverse the leading sign
setValue(-years, -months, -days, -hours, -minutes, -seconds);
}
else
{
setValue(years, months, days, hours, minutes, seconds);
}
}
/**
* Set all values of this interval to the specified values
*/
public void setValue(int years, int months, int days, int hours, int minutes, double seconds)
{
setYears(years);
setMonths(months);
setDays(days);
setHours(hours);
setMinutes(minutes);
setSeconds(seconds);
}
/**
* Returns the stored interval information as a string
*
* @return String represented interval
*/
public String getValue()
{
return years + " years " +
months + " mons " +
days + " days " +
hours + " hours " +
minutes + " mins " +
secondsFormat.format(seconds) + " secs";
}
/**
* Returns the years represented by this interval
*/
public int getYears()
{
return years;
}
/**
* Set the years of this interval to the specified value
*/
public void setYears(int years)
{
this.years = years;
}
/**
* Returns the months represented by this interval
*/
public int getMonths()
{
return months;
}
/**
* Set the months of this interval to the specified value
*/
public void setMonths(int months)
{
this.months = months;
}
/**
* Returns the days represented by this interval
*/
public int getDays()
{
return days;
}
/**
* Set the days of this interval to the specified value
*/
public void setDays(int days)
{
this.days = days;
}
/**
* Returns the hours represented by this interval
*/
public int getHours()
{
return hours;
}
/**
* Set the hours of this interval to the specified value
*/
public void setHours(int hours)
{
this.hours = hours;
}
/**
* Returns the minutes represented by this interval
*/
public int getMinutes()
{
return minutes;
}
/**
* Set the minutes of this interval to the specified value
*/
public void setMinutes(int minutes)
{
this.minutes = minutes;
}
/**
* Returns the seconds represented by this interval
*/
public double getSeconds()
{
return seconds;
}
/**
* Set the seconds of this interval to the specified value
*/
public void setSeconds(double seconds)
{
this.seconds = seconds;
}
/**
* Rolls this interval on a given calendar
*
* @param cal Calendar instance to add to
*/
public void add(Calendar cal)
{
// Avoid precision loss
// Be aware postgres doesn't return more than 60 seconds - no overflow can happen
final int microseconds = (int)(getSeconds() * 1000000.0);
final int milliseconds = (microseconds + ((microseconds < 0) ? -500 : 500)) / 1000;
cal.add(Calendar.MILLISECOND, milliseconds);
cal.add(Calendar.MINUTE, getMinutes());
cal.add(Calendar.HOUR, getHours());
cal.add(Calendar.DAY_OF_MONTH, getDays());
cal.add(Calendar.MONTH, getMonths());
cal.add(Calendar.YEAR, getYears());
}
/**
* Rolls this interval on a given date
*
* @param date Date instance to add to
*/
public void add(Date date)
{
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
add(cal);
date.setTime(cal.getTime().getTime());
}
/**
* Add this interval's value to the passed interval.
* This is backwards to what I would expect, but
* this makes it match the other existing add methods.
*/
public void add(PGInterval interval)
{
interval.setYears(interval.getYears() + getYears());
interval.setMonths(interval.getMonths() + getMonths());
interval.setDays(interval.getDays() + getDays());
interval.setHours(interval.getHours() + getHours());
interval.setMinutes(interval.getMinutes() + getMinutes());
interval.setSeconds(interval.getSeconds() + getSeconds());
}
/**
* Scale this interval by an integer factor. The server
* can scale by arbitrary factors, but that would require
* adjusting the call signatures for all the existing methods
* like getDays() or providing our own justification of fractional
* intervals. Neither of these seem like a good idea without a
* strong use case.
*/
public void scale(int factor)
{
setYears(factor * getYears());
setMonths(factor * getMonths());
setDays(factor * getDays());
setHours(factor * getHours());
setMinutes(factor * getMinutes());
setSeconds(factor * getSeconds());
}
/**
* Returns integer value of value or 0 if value is null
*
* @param value integer as string value
* @return integer parsed from string value
* @throws NumberFormatException if the string contains invalid chars
*/
private int nullSafeIntGet(String value)
throws NumberFormatException
{
return (value == null) ? 0 : Integer.parseInt(value);
}
/**
* Returns double value of value or 0 if value is null
*
* @param value double as string value
* @return double parsed from string value
* @throws NumberFormatException if the string contains invalid chars
*/
private double nullSafeDoubleGet(String value)
throws NumberFormatException
{
return (value == null) ? 0 : Double.parseDouble(value);
}
/**
* Returns whether an object is equal to this one or not
*
* @param obj Object to compare with
* @return true if the two intervals are identical
*/
public boolean equals(Object obj)
{
if (obj == null)
return false;
if (obj == this)
return true;
if (!(obj instanceof PGInterval))
return false;
final PGInterval pgi = (PGInterval)obj;
return
pgi.years == years &&
pgi.months == months &&
pgi.days == days &&
pgi.hours == hours &&
pgi.minutes == minutes &&
Double.doubleToLongBits(pgi.seconds) == Double.doubleToLongBits(seconds);
}
/**
* Returns a hashCode for this object
*
* @return hashCode
*/
public int hashCode()
{
return ((((((7 * 31 +
(int)Double.doubleToLongBits(seconds)) * 31 +
minutes) * 31 +
hours) * 31 +
days) * 31 +
months) * 31 +
years) * 31;
}
}
|