下面列出了com.google.gson.internal.LazilyParsedNumber#org.joda.time.ReadablePeriod 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void printTo(Writer out, ReadablePeriod period, Locale locale) throws IOException {
PeriodPrinter before = iBeforePrinter;
PeriodPrinter after = iAfterPrinter;
before.printTo(out, period, locale);
if (iUseBefore) {
if (before.countFieldsToPrint(period, 1, locale) > 0) {
if (iUseAfter) {
int afterCount = after.countFieldsToPrint(period, 2, locale);
if (afterCount > 0) {
out.write(afterCount > 1 ? iText : iFinalText);
}
} else {
out.write(iText);
}
}
} else if (iUseAfter && after.countFieldsToPrint(period, 1, locale) > 0) {
out.write(iText);
}
after.printTo(out, period, locale);
}
public int calculatePrintedLength(ReadablePeriod period, Locale locale) {
PeriodPrinter before = iBeforePrinter;
PeriodPrinter after = iAfterPrinter;
int sum = before.calculatePrintedLength(period, locale)
+ after.calculatePrintedLength(period, locale);
if (iUseBefore) {
if (before.countFieldsToPrint(period, 1, locale) > 0) {
if (iUseAfter) {
int afterCount = after.countFieldsToPrint(period, 2, locale);
if (afterCount > 0) {
sum += (afterCount > 1 ? iText : iFinalText).length();
}
} else {
sum += iText.length();
}
}
} else if (iUseAfter && after.countFieldsToPrint(period, 1, locale) > 0) {
sum += iText.length();
}
return sum;
}
public void printTo(StringBuffer buf, ReadablePeriod period, Locale locale) {
PeriodPrinter before = iBeforePrinter;
PeriodPrinter after = iAfterPrinter;
before.printTo(buf, period, locale);
if (iUseBefore) {
if (before.countFieldsToPrint(period, 1, locale) > 0) {
if (iUseAfter) {
int afterCount = after.countFieldsToPrint(period, 2, locale);
if (afterCount > 0) {
buf.append(afterCount > 1 ? iText : iFinalText);
}
} else {
buf.append(iText);
}
}
} else if (iUseAfter && after.countFieldsToPrint(period, 1, locale) > 0) {
buf.append(iText);
}
after.printTo(buf, period, locale);
}
public void printTo(Writer out, ReadablePeriod period, Locale locale) throws IOException {
PeriodPrinter before = iBeforePrinter;
PeriodPrinter after = iAfterPrinter;
before.printTo(out, period, locale);
if (iUseBefore) {
if (before.countFieldsToPrint(period, 1, locale) > 0) {
if (iUseAfter) {
int afterCount = after.countFieldsToPrint(period, 2, locale);
if (afterCount > 0) {
out.write(afterCount > 1 ? iText : iFinalText);
}
} else {
out.write(iText);
}
}
} else if (iUseAfter && after.countFieldsToPrint(period, 1, locale) > 0) {
out.write(iText);
}
after.printTo(out, period, locale);
}
/**
* Adds the fields from another period.
*
* @param values the array of values to update
* @param period the period to add from, not null
* @return the updated values
* @throws IllegalArgumentException if an unsupported field's value is non-zero
*/
protected int[] addPeriodInto(int[] values, ReadablePeriod period) {
for (int i = 0, isize = period.size(); i < isize; i++) {
DurationFieldType type = period.getFieldType(i);
int value = period.getValue(i);
if (value != 0) {
int index = indexOf(type);
if (index == -1) {
throw new IllegalArgumentException(
"Period does not support field '" + type.getName() + "'");
} else {
values[index] = FieldUtils.safeAdd(getValue(index), value);
}
}
}
return values;
}
public void testGetPeriodConverter() {
PeriodConverter c = ConverterManager.getInstance().getPeriodConverter(new Period(1, 2, 3, 4, 5, 6, 7, 8));
assertEquals(ReadablePeriod.class, c.getSupportedType());
c = ConverterManager.getInstance().getPeriodConverter(new Duration(123L));
assertEquals(ReadableDuration.class, c.getSupportedType());
c = ConverterManager.getInstance().getPeriodConverter(new Interval(0L, 1000L));
assertEquals(ReadableInterval.class, c.getSupportedType());
c = ConverterManager.getInstance().getPeriodConverter("");
assertEquals(String.class, c.getSupportedType());
c = ConverterManager.getInstance().getPeriodConverter(null);
assertEquals(null, c.getSupportedType());
try {
ConverterManager.getInstance().getPeriodConverter(Boolean.TRUE);
fail();
} catch (IllegalArgumentException ex) {}
}
/**
* Gets the values of a period from an interval.
*
* @param period the period instant to use
* @param duration the duration to query
* @return the values of the period extracted from the duration
*/
public int[] get(ReadablePeriod period, long duration) {
int size = period.size();
int[] values = new int[size];
if (duration != 0) {
long current = 0;
for (int i = 0; i < size; i++) {
DurationField field = period.getFieldType(i).getField(this);
if (field.isPrecise()) {
int value = field.getDifference(duration, current);
current = field.add(current, value);
values[i] = value;
}
}
}
return values;
}
public int calculatePrintedLength(ReadablePeriod period, Locale locale) {
PeriodPrinter before = iBeforePrinter;
PeriodPrinter after = iAfterPrinter;
int sum = before.calculatePrintedLength(period, locale)
+ after.calculatePrintedLength(period, locale);
if (iUseBefore) {
if (before.countFieldsToPrint(period, 1, locale) > 0) {
if (iUseAfter) {
int afterCount = after.countFieldsToPrint(period, 2, locale);
if (afterCount > 0) {
sum += (afterCount > 1 ? iText : iFinalText).length();
}
} else {
sum += iText.length();
}
}
} else if (iUseAfter && after.countFieldsToPrint(period, 1, locale) > 0) {
sum += iText.length();
}
return sum;
}
public void printTo(StringBuffer buf, ReadablePeriod period, Locale locale) {
PeriodPrinter before = iBeforePrinter;
PeriodPrinter after = iAfterPrinter;
before.printTo(buf, period, locale);
if (iUseBefore) {
if (before.countFieldsToPrint(period, 1, locale) > 0) {
if (iUseAfter) {
int afterCount = after.countFieldsToPrint(period, 2, locale);
if (afterCount > 0) {
buf.append(afterCount > 1 ? iText : iFinalText);
}
} else {
buf.append(iText);
}
}
} else if (iUseAfter && after.countFieldsToPrint(period, 1, locale) > 0) {
buf.append(iText);
}
after.printTo(buf, period, locale);
}
/**
* Calculates the number of whole units between the two specified partial datetimes.
* <p>
* The two partials must contain the same fields, for example you can specify
* two <code>LocalDate</code> objects.
*
* @param start the start partial date, validated to not be null
* @param end the end partial date, validated to not be null
* @param zeroInstance the zero instance constant, must not be null
* @return the period
* @throws IllegalArgumentException if the partials are null or invalid
*/
protected static int between(ReadablePartial start, ReadablePartial end, ReadablePeriod zeroInstance) {
if (start == null || end == null) {
throw new IllegalArgumentException("ReadablePartial objects must not be null");
}
if (start.size() != end.size()) {
throw new IllegalArgumentException("ReadablePartial objects must have the same set of fields");
}
for (int i = 0, isize = start.size(); i < isize; i++) {
if (start.getFieldType(i) != end.getFieldType(i)) {
throw new IllegalArgumentException("ReadablePartial objects must have the same set of fields");
}
}
if (DateTimeUtils.isContiguous(start) == false) {
throw new IllegalArgumentException("ReadablePartial objects must be contiguous");
}
Chronology chrono = DateTimeUtils.getChronology(start.getChronology()).withUTC();
int[] values = chrono.get(zeroInstance, chrono.set(start, START_1972), chrono.set(end, START_1972));
return values[0];
}
/**
* Creates a new instance representing the number of complete standard length units
* in the specified period.
* <p>
* This factory method converts all fields from the period to hours using standardised
* durations for each field. Only those fields which have a precise duration in
* the ISO UTC chronology can be converted.
* <ul>
* <li>One week consists of 7 days.
* <li>One day consists of 24 hours.
* <li>One hour consists of 60 minutes.
* <li>One minute consists of 60 seconds.
* <li>One second consists of 1000 milliseconds.
* </ul>
* Months and Years are imprecise and periods containing these values cannot be converted.
*
* @param period the period to get the number of hours from, must not be null
* @param millisPerUnit the number of milliseconds in one standard unit of this period
* @throws IllegalArgumentException if the period contains imprecise duration values
*/
protected static int standardPeriodIn(ReadablePeriod period, long millisPerUnit) {
if (period == null) {
return 0;
}
Chronology iso = ISOChronology.getInstanceUTC();
long duration = 0L;
for (int i = 0; i < period.size(); i++) {
int value = period.getValue(i);
if (value != 0) {
DurationField field = period.getFieldType(i).getField(iso);
if (field.isPrecise() == false) {
throw new IllegalArgumentException(
"Cannot convert period to duration as " + field.getName() +
" is not precise in the period " + period);
}
duration = FieldUtils.safeAdd(duration, FieldUtils.safeMultiply(field.getUnitMillis(), value));
}
}
return FieldUtils.safeToInt(duration / millisPerUnit);
}
/**
* Gets the values of a period from an interval.
*
* @param period the period instant to use
* @param duration the duration to query
* @return the values of the period extracted from the duration
*/
public int[] get(ReadablePeriod period, long duration) {
int size = period.size();
int[] values = new int[size];
if (duration != 0) {
long current = 0;
for (int i = 0; i < size; i++) {
DurationField field = period.getFieldType(i).getField(this);
if (field.isPrecise()) {
int value = field.getDifference(duration, current);
current = field.add(current, value);
values[i] = value;
}
}
}
return values;
}
public void printTo(StringBuffer buf, ReadablePeriod period, Locale locale) {
PeriodPrinter before = iBeforePrinter;
PeriodPrinter after = iAfterPrinter;
before.printTo(buf, period, locale);
if (iUseBefore) {
if (before.countFieldsToPrint(period, 1, locale) > 0) {
if (iUseAfter) {
int afterCount = after.countFieldsToPrint(period, 2, locale);
if (afterCount > 0) {
buf.append(afterCount > 1 ? iText : iFinalText);
}
} else {
buf.append(iText);
}
}
} else if (iUseAfter && after.countFieldsToPrint(period, 1, locale) > 0) {
buf.append(iText);
}
after.printTo(buf, period, locale);
}
public void printTo(Writer out, ReadablePeriod period, Locale locale) throws IOException {
PeriodPrinter before = iBeforePrinter;
PeriodPrinter after = iAfterPrinter;
before.printTo(out, period, locale);
if (iUseBefore) {
if (before.countFieldsToPrint(period, 1, locale) > 0) {
if (iUseAfter) {
int afterCount = after.countFieldsToPrint(period, 2, locale);
if (afterCount > 0) {
out.write(afterCount > 1 ? iText : iFinalText);
}
} else {
out.write(iText);
}
}
} else if (iUseAfter && after.countFieldsToPrint(period, 1, locale) > 0) {
out.write(iText);
}
after.printTo(out, period, locale);
}
public int calculatePrintedLength(ReadablePeriod period, Locale locale) {
long valueLong = getFieldValue(period);
if (valueLong == Long.MAX_VALUE) {
return 0;
}
int sum = Math.max(FormatUtils.calculateDigitCount(valueLong), iMinPrintedDigits);
if (iFieldType >= SECONDS_MILLIS) {
// valueLong contains the seconds and millis fields
// the minimum output is 0.000, which is 4 or 5 digits with a negative
sum = (valueLong < 0 ? Math.max(sum, 5) : Math.max(sum, 4));
// plus one for the decimal point
sum++;
if (iFieldType == SECONDS_OPTIONAL_MILLIS &&
(Math.abs(valueLong) % DateTimeConstants.MILLIS_PER_SECOND) == 0) {
sum -= 4; // remove three digits and decimal point
}
// reset valueLong to refer to the seconds part for the prefic/suffix calculation
valueLong = valueLong / DateTimeConstants.MILLIS_PER_SECOND;
}
int value = (int) valueLong;
if (iPrefix != null) {
sum += iPrefix.calculatePrintedLength(value);
}
if (iSuffix != null) {
sum += iSuffix.calculatePrintedLength(value);
}
return sum;
}
/**
* Prints a ReadablePeriod to a StringBuffer.
*
* @param buf the formatted period is appended to this buffer
* @param period the period to format, not null
*/
public void printTo(StringBuffer buf, ReadablePeriod period) {
checkPrinter();
checkPeriod(period);
getPrinter().printTo(buf, period, iLocale);
}
boolean isZero(ReadablePeriod period) {
for (int i = 0, isize = period.size(); i < isize; i++) {
if (period.getValue(i) != 0) {
return false;
}
}
return true;
}
/**
* Sets all the fields of this period from another.
*
* @param period the period to copy from, not null
* @throws IllegalArgumentException if an unsupported field's value is non-zero
*/
protected void setPeriod(ReadablePeriod period) {
if (period == null) {
setValues(new int[size()]);
} else {
setPeriodInternal(period);
}
}
public int countFieldsToPrint(ReadablePeriod period, int stopAt, Locale locale) {
int sum = iBeforePrinter.countFieldsToPrint(period, stopAt, locale);
if (sum < stopAt) {
sum += iAfterPrinter.countFieldsToPrint(period, stopAt, locale);
}
return sum;
}
public int countFieldsToPrint(ReadablePeriod period, int stopAt, Locale locale) {
int sum = 0;
PeriodPrinter[] printers = iPrinters;
for (int i=printers.length; sum < stopAt && --i>=0; ) {
sum += printers[i].countFieldsToPrint(period, Integer.MAX_VALUE, locale);
}
return sum;
}
public int calculatePrintedLength(ReadablePeriod period, Locale locale) {
int sum = 0;
PeriodPrinter[] printers = iPrinters;
for (int i=printers.length; --i>=0; ) {
sum += printers[i].calculatePrintedLength(period, locale);
}
return sum;
}
/**
* Constructs an interval from a start instant and a time period.
* <p>
* When forming the interval, the chronology from the instant is used
* if present, otherwise the chronology of the period is used.
*
* @param start start of this interval, null means now
* @param period the period of this interval, null means zero length
* @throws IllegalArgumentException if the end is before the start
* @throws ArithmeticException if the end instant exceeds the capacity of a long
*/
protected BaseInterval(ReadableInstant start, ReadablePeriod period) {
super();
Chronology chrono = DateTimeUtils.getInstantChronology(start);
iChronology = chrono;
iStartMillis = DateTimeUtils.getInstantMillis(start);
if (period == null) {
iEndMillis = iStartMillis;
} else {
iEndMillis = chrono.add(period, iStartMillis, 1);
}
checkInterval(iStartMillis, iEndMillis);
}
public void printTo(StringBuffer buf, ReadablePeriod period, Locale locale) {
long valueLong = getFieldValue(period);
if (valueLong == Long.MAX_VALUE) {
return;
}
int value = (int) valueLong;
if (iFieldType >= SECONDS_MILLIS) {
value = (int) (valueLong / DateTimeConstants.MILLIS_PER_SECOND);
}
if (iPrefix != null) {
iPrefix.printTo(buf, value);
}
int minDigits = iMinPrintedDigits;
if (minDigits <= 1) {
FormatUtils.appendUnpaddedInteger(buf, value);
} else {
FormatUtils.appendPaddedInteger(buf, value, minDigits);
}
if (iFieldType >= SECONDS_MILLIS) {
int dp = (int) (Math.abs(valueLong) % DateTimeConstants.MILLIS_PER_SECOND);
if (iFieldType == SECONDS_MILLIS || dp > 0) {
buf.append('.');
FormatUtils.appendPaddedInteger(buf, dp, 3);
}
}
if (iSuffix != null) {
iSuffix.printTo(buf, value);
}
}
public void printTo(Writer out, ReadablePeriod period, Locale locale) throws IOException {
long valueLong = getFieldValue(period);
if (valueLong == Long.MAX_VALUE) {
return;
}
int value = (int) valueLong;
if (iFieldType >= SECONDS_MILLIS) {
value = (int) (valueLong / DateTimeConstants.MILLIS_PER_SECOND);
}
if (iPrefix != null) {
iPrefix.printTo(out, value);
}
int minDigits = iMinPrintedDigits;
if (minDigits <= 1) {
FormatUtils.writeUnpaddedInteger(out, value);
} else {
FormatUtils.writePaddedInteger(out, value, minDigits);
}
if (iFieldType >= SECONDS_MILLIS) {
int dp = (int) (Math.abs(valueLong) % DateTimeConstants.MILLIS_PER_SECOND);
if (iFieldType == SECONDS_MILLIS || dp > 0) {
out.write('.');
FormatUtils.writePaddedInteger(out, dp, 3);
}
}
if (iSuffix != null) {
iSuffix.printTo(out, value);
}
}
/**
* Adds the period to the instant, specifying the number of times to add.
*
* @param period the period to add, null means add nothing
* @param instant the instant to add to
* @param scalar the number of times to add
* @return the updated instant
*/
public long add(ReadablePeriod period, long instant, int scalar) {
if (scalar != 0 && period != null) {
for (int i = 0, isize = period.size(); i < isize; i++) {
long value = period.getValue(i); // use long to allow for multiplication (fits OK)
if (value != 0) {
instant = period.getFieldType(i).getField(this).add(instant, value * scalar);
}
}
}
return instant;
}
@Override
public Object create(Object request, SpecimenContext context) {
if (!(request.equals(ReadablePeriod.class) || request.equals(ReadWritablePeriod.class)))
return new NoSpecimen();
DateTime dateA = (DateTime) context.resolve(DateTime.class);
DateTime dateB = (DateTime) context.resolve(DateTime.class);
if (dateA.isBefore(dateB))
return new MutablePeriod(dateA, dateB);
else
return new MutablePeriod(dateB, dateA);
}
/**
* Prints a ReadablePeriod to a Writer.
*
* @param out the formatted period is written out
* @param period the period to format, not null
*/
public void printTo(Writer out, ReadablePeriod period) throws IOException {
checkPrinter();
checkPeriod(period);
getPrinter().printTo(out, period, iLocale);
}
public int calculatePrintedLength(ReadablePeriod period, Locale locale) {
int sum = 0;
PeriodPrinter[] printers = iPrinters;
for (int i=printers.length; --i>=0; ) {
sum += printers[i].calculatePrintedLength(period, locale);
}
return sum;
}
public void printTo(StringBuffer buf, ReadablePeriod period, Locale locale) {
PeriodPrinter[] printers = iPrinters;
int len = printers.length;
for (int i=0; i<len; i++) {
printers[i].printTo(buf, period, locale);
}
}
public void printTo(Writer out, ReadablePeriod period, Locale locale) throws IOException {
PeriodPrinter[] printers = iPrinters;
int len = printers.length;
for (int i=0; i<len; i++) {
printers[i].printTo(out, period, locale);
}
}