/** Tests the getters of current unit system and speed view. */ @Test public void testGetSettings() { FormatUtils formatUtils = new FormatUtils(FormatUtils.UnitSystem.English, FormatUtils.SpeedView.MinutesPerDistance); assertEquals(FormatUtils.UnitSystem.English, formatUtils.getUnitSystem()); assertEquals(FormatUtils.SpeedView.MinutesPerDistance, formatUtils.getSpeedView()); }
/** Tests the appropriate method. */ @Test public void testSeconds2MinuteTimeString() { FormatUtils formatUtils = new FormatUtils(FormatUtils.UnitSystem.Metric, FormatUtils.SpeedView.DistancePerHour); assertEquals(formatUtils.seconds2MinuteTimeString(0), "00:00"); assertEquals(formatUtils.seconds2MinuteTimeString(3599), "59:59"); assertEquals(formatUtils.seconds2MinuteTimeString(3601), "60:01"); assertEquals(formatUtils.seconds2MinuteTimeString(365145), "6085:45"); }
/** Tests the appropriate method. */ @Test public void testMinutes2TimeString() { FormatUtils formatUtils = new FormatUtils(FormatUtils.UnitSystem.Metric, FormatUtils.SpeedView.DistancePerHour); assertEquals(formatUtils.minutes2TimeString(0), "00:00"); assertEquals(formatUtils.minutes2TimeString(9), "00:09"); assertEquals(formatUtils.minutes2TimeString(70), "01:10"); assertEquals(formatUtils.minutes2TimeString(3600), "60:00"); assertEquals(formatUtils.minutes2TimeString(7199), "119:59"); }
@Override public String toString() { final StringBuffer b = new StringBuffer(); b.append("GIF video format:"); if (FormatUtils.specified(size)) b.append(" size = " + size.width + "x" + size.height); if (FormatUtils.specified(frameRate)) b.append(" FrameRate = " + frameRate); if (FormatUtils.specified(maxDataLength)) b.append(" maxDataLength = " + maxDataLength); if (FormatUtils.specified(dataType)) b.append(" dataType = " + dataType); return b.toString(); }
/** * Returns a format-String that can be used to generate a String representation of an array using * the String.format method. * * @param arr Array for which a String representation is desired * @param precision Desired precision for <code>Float</code> and <code>Double</code> elements * @return Format-String for <code>arr</code> * @see Formatter * @see String#format(String, Object...) */ private static <T> String getFormat(T[] arr, int precision, char valueSeparation) { StringBuilder builder = new StringBuilder(); builder.append('['); for (int i = 0; i < arr.length - 1; i++) { builder.append(FormatUtils.getFormat(arr[i], precision)); builder.append(valueSeparation); } builder.append(FormatUtils.getFormat(arr[arr.length - 1], precision)); builder.append(']'); return builder.toString(); }
/** * Tests that distanceToStringWithoutUnitName works as expected (most cases are allready tested in * testDistanceToString()). */ @Test public void testDistanceToStringWithoutUnitName() { FormatUtils formatUtils = new FormatUtils(FormatUtils.UnitSystem.Metric, FormatUtils.SpeedView.DistancePerHour); assertEquals("100", formatUtils.distanceToStringWithoutUnitName(100f, 0)); assertEquals("100.56", formatUtils.distanceToStringWithoutUnitName(100.555f, 2)); formatUtils = new FormatUtils(FormatUtils.UnitSystem.English, FormatUtils.SpeedView.DistancePerHour); assertEquals("62", formatUtils.distanceToStringWithoutUnitName(100f, 0)); assertEquals("62.45", formatUtils.distanceToStringWithoutUnitName(100.50f, 2)); }
/** Tests that temperatureToString works as expected. */ @Test public void testTemperatureToString() { FormatUtils formatUtils = new FormatUtils(FormatUtils.UnitSystem.Metric, FormatUtils.SpeedView.DistancePerHour); assertEquals("-5 C", formatUtils.temperatureToString((short) -5)); assertEquals("100 C", formatUtils.temperatureToString((short) 100)); assertEquals("1,234 C", formatUtils.temperatureToString((short) 1234)); formatUtils = new FormatUtils(FormatUtils.UnitSystem.English, FormatUtils.SpeedView.DistancePerHour); assertEquals("212 F", formatUtils.temperatureToString((short) 100)); }
/** * Parses a date-time from the given text, returning a new MutableDateTime. * * <p>The parse will use the zone and chronology specified on this formatter. * * <p>If the text contains a time zone string then that will be taken into account in adjusting * the time of day as follows. If the {@link #withOffsetParsed()} has been called, then the * resulting DateTime will have a fixed offset based on the parsed time zone. Otherwise the * resulting DateTime will have the zone of this formatter, but the parsed zone may have caused * the time to be adjusted. * * @param text the text to parse, not null * @return the parsed date-time, never null * @throws UnsupportedOperationException if parsing is not supported * @throws IllegalArgumentException if the text to parse is invalid */ public MutableDateTime parseMutableDateTime(String text) { InternalParser parser = requireParser(); Chronology chrono = selectChronology(null); DateTimeParserBucket bucket = new DateTimeParserBucket(0, chrono, iLocale, iPivotYear, iDefaultYear); int newPos = parser.parseInto(bucket, text, 0); if (newPos >= 0) { if (newPos >= text.length()) { long millis = bucket.computeMillis(true, text); if (iOffsetParsed && bucket.getOffsetInteger() != null) { int parsedOffset = bucket.getOffsetInteger(); DateTimeZone parsedZone = DateTimeZone.forOffsetMillis(parsedOffset); chrono = chrono.withZone(parsedZone); } else if (bucket.getZone() != null) { chrono = chrono.withZone(bucket.getZone()); } MutableDateTime dt = new MutableDateTime(millis, chrono); if (iZone != null) { dt.setZone(iZone); } return dt; } } else { newPos = ~newPos; } throw new IllegalArgumentException(FormatUtils.createErrorMessage(text, newPos)); }
/** Tests that distanceToString works as expected. */ @Test public void testDistanceToString() { FormatUtils formatUtils = new FormatUtils(FormatUtils.UnitSystem.Metric, FormatUtils.SpeedView.DistancePerHour); assertEquals("0 km", formatUtils.distanceToString(0f, 0)); assertEquals("100 km", formatUtils.distanceToString(100f, 0)); assertEquals("100 km", formatUtils.distanceToString(100.0f, 2)); assertEquals("100.55 km", formatUtils.distanceToString(100.55f, 2)); assertEquals("100.56 km", formatUtils.distanceToString(100.555f, 2)); assertEquals("100,234.55 km", formatUtils.distanceToString(100234.55f, 2)); formatUtils = new FormatUtils(FormatUtils.UnitSystem.English, FormatUtils.SpeedView.DistancePerHour); assertEquals("0 m", formatUtils.distanceToString(0f, 0)); assertEquals("62 m", formatUtils.distanceToString(100f, 0)); assertEquals("62.45 m", formatUtils.distanceToString(100.50f, 2)); }
/** * Tests that speedToStringWithoutUnitName works as expected (most cases are allready tested in * testSpeedToString()). */ @Test public void testSpeedToStringWithoutUnitName() { FormatUtils formatUtils = new FormatUtils(FormatUtils.UnitSystem.Metric, FormatUtils.SpeedView.DistancePerHour); assertEquals("0", formatUtils.speedToString(0f, 0)); assertEquals("100", formatUtils.speedToStringWithoutUnitName(100f, 0)); assertEquals("100.56", formatUtils.speedToStringWithoutUnitName(100.555f, 2)); formatUtils = new FormatUtils(FormatUtils.UnitSystem.Metric, FormatUtils.SpeedView.MinutesPerDistance); assertEquals("05:00", formatUtils.speedToStringWithoutUnitName(12f, 0)); assertEquals("N/A", formatUtils.speedToStringWithoutUnitName(0f, 0)); formatUtils = new FormatUtils(FormatUtils.UnitSystem.English, FormatUtils.SpeedView.DistancePerHour); assertEquals("0", formatUtils.speedToString(0f, 0)); assertEquals("62.45", formatUtils.speedToStringWithoutUnitName(100.50f, 2)); formatUtils = new FormatUtils(FormatUtils.UnitSystem.English, FormatUtils.SpeedView.MinutesPerDistance); assertEquals("08:02", formatUtils.speedToStringWithoutUnitName(12f, 0)); assertEquals("N/A", formatUtils.speedToStringWithoutUnitName(0f, 0)); }
/** * Parses only the local date-time from the given text, returning a new LocalDateTime. * * <p>This will parse the text fully according to the formatter, using the UTC zone. Once parsed, * only the local date-time will be used. This means that any parsed time-zone or offset field is * completely ignored. It also means that the zone and offset-parsed settings are ignored. * * @param text the text to parse, not null * @return the parsed date-time, never null * @throws UnsupportedOperationException if parsing is not supported * @throws IllegalArgumentException if the text to parse is invalid * @since 2.0 */ public LocalDateTime parseLocalDateTime(String text) { InternalParser parser = requireParser(); Chronology chrono = selectChronology(null).withUTC(); // always use UTC, avoiding DST gaps DateTimeParserBucket bucket = new DateTimeParserBucket(0, chrono, iLocale, iPivotYear, iDefaultYear); int newPos = parser.parseInto(bucket, text, 0); if (newPos >= 0) { if (newPos >= text.length()) { long millis = bucket.computeMillis(true, text); if (bucket.getOffsetInteger() != null) { // treat withOffsetParsed() as being true int parsedOffset = bucket.getOffsetInteger(); DateTimeZone parsedZone = DateTimeZone.forOffsetMillis(parsedOffset); chrono = chrono.withZone(parsedZone); } else if (bucket.getZone() != null) { chrono = chrono.withZone(bucket.getZone()); } return new LocalDateTime(millis, chrono); } } else { newPos = ~newPos; } throw new IllegalArgumentException(FormatUtils.createErrorMessage(text, newPos)); }
@Override public boolean matches(Format format) { if (!super.matches(format)) { // if (getClass() == FormatUtils.audioFormatClass) { // FormatTraceUtils.traceMatches(this, format, false); // otherwise // let subclass trace // } return false; } if (!(format instanceof AudioFormat)) { final boolean result = true; // if (getClass() == FormatUtils.audioFormatClass){ // FormatTraceUtils.traceMatches(this, format, result); // } return result; } final AudioFormat oCast = (AudioFormat) format; final boolean result = FormatUtils.matches(this.sampleRate, oCast.sampleRate) && FormatUtils.matches(this.sampleSizeInBits, oCast.sampleSizeInBits) && FormatUtils.matches(this.channels, oCast.channels) && FormatUtils.matches(this.endian, oCast.endian) && FormatUtils.matches(this.signed, oCast.signed) && FormatUtils.matches(this.frameSizeInBits, oCast.frameSizeInBits) && FormatUtils.matches(this.frameRate, oCast.frameRate); // if (getClass() == FormatUtils.audioFormatClass){ // FormatTraceUtils.traceMatches(this, format, result); // otherwise let // subclass trace // } return result; }
/** Tests that speedToString works as expected. */ @Test public void testSpeedToString() { FormatUtils formatUtils = new FormatUtils(FormatUtils.UnitSystem.Metric, FormatUtils.SpeedView.DistancePerHour); assertEquals("0", formatUtils.speedToString(0f, 0)); assertEquals("100 km/h", formatUtils.speedToString(100f, 0)); assertEquals("100 km/h", formatUtils.speedToString(100.0f, 2)); assertEquals("100.55 km/h", formatUtils.speedToString(100.55f, 2)); assertEquals("100.56 km/h", formatUtils.speedToString(100.555f, 2)); assertEquals("100,234.55 km/h", formatUtils.speedToString(100234.55f, 2)); formatUtils = new FormatUtils(FormatUtils.UnitSystem.Metric, FormatUtils.SpeedView.MinutesPerDistance); assertEquals("05:00 min/km", formatUtils.speedToString(12f, 0)); assertEquals("N/A", formatUtils.speedToString(0f, 0)); formatUtils = new FormatUtils(FormatUtils.UnitSystem.English, FormatUtils.SpeedView.DistancePerHour); assertEquals("0", formatUtils.speedToString(0f, 0)); assertEquals("62 mph", formatUtils.speedToString(100f, 0)); assertEquals("62.45 mph", formatUtils.speedToString(100.50f, 2)); formatUtils = new FormatUtils(FormatUtils.UnitSystem.English, FormatUtils.SpeedView.MinutesPerDistance); assertEquals("08:02 min/m", formatUtils.speedToString(12f, 0)); assertEquals("N/A", formatUtils.speedToString(0f, 0)); }
@Override public Object clone() { return new GIFFormat(FormatUtils.clone(size), maxDataLength, dataType, frameRate); }
public static String computeTime(String dateString, String val, String timeUnit) { DateTime date = new DateTime(); int value = 0; boolean isLong = false; if (!Strings.isEmpty(val) && !Strings.isEmpty(dateString) && !Strings.isEmpty(timeUnit)) { if (dateString.substring(dateString.indexOf("T") + 1, dateString.length()).length() == 0) { dateString = dateString + DEFAULT_TIME_FORMAT; } date = new DateTime(dateString); if (val.indexOf(".") > 0) { val = val.substring(0, val.indexOf(".")); } Long l = new Long(val); if ((l.longValue() < 0 && -l.longValue() > 32768) || (l.longValue() > 0 && l.longValue() > 32768)) { isLong = true; } if (!isLong) { value = Integer.parseInt(val); } if (!Strings.isEmpty(timeUnit)) { if (timeUnit.equals("d")) { if (!isLong) { if (value > 0) { date = date.plusDays(value); } else { date = date.minusDays(-value); } } else { if (l.longValue() < 0) { date = date.minus(-l.longValue() * 24 * 60 * 60 * 1000); } else { date = date.plus(l.longValue() * 24 * 60 * 60 * 1000); } } } else if (timeUnit.equals(HOUR)) { if (!isLong) { if (value > 0) { date = date.plusHours(value); } else { date = date.minusHours(-value); } } else { if (l.longValue() < 0) { date = date.minus(-l.longValue() * 60 * 60 * 1000); } else { date = date.plus(l.longValue() * 60 * 60 * 1000); } } } else if (timeUnit.equals(MIN)) { if (!isLong) { if (value > 0) { date = date.plusMinutes(value); } else { date = date.minusMinutes(-value); } } else { if (l.longValue() < 0) { date = date.minus(-l.longValue() * 60 * 1000); } else { date = date.plus(l.longValue() * 60 * 1000); } } } else if (timeUnit.equals(SEC)) { if (!isLong) { if (value > 0) { date = date.plusSeconds(value); } else { date = date.minusSeconds(-value); } } else { if (l.longValue() < 0) { date = date.minus(-l.longValue() * 1000); } else { date = date.plus(l.longValue() * 1000); } } } } } return FormatUtils.formatDateTimeToISO(date.toDate()); }
/** * Overrides default toString. Flag indicates if output should be equal to parsed string: * dolbyac3, 2, 1, 2; * * @param asInput * <ul> * <li><b>true</b> - will return in the exact same way it has been fed: dolbyac3, 2, 1, 2 - * this format is defined in rtp media format * <li><b>false</b> - will return in format like: dolbyac3, 2.0 Hz, 1-bit, Stereo, Unsigned, * 6.0 frame rate * </ul> * * @return */ public String toString(boolean asInput) { // examples: // dolbyac3, Unknown Sample Rate // dolbyac3, 2.0 Hz, 1-bit, Stereo, Unsigned, 6.0 frame rate, // FrameSize=5 bits // dolbyac3, 2.0 Hz, 1-bit, 0-channel, Unsigned, 6.0 frame rate, // FrameSize=5 bits // dolbyac3, 2.0 Hz, 1-bit, 3-channel, Unsigned, 6.0 frame rate, // FrameSize=5 bits // TODO: use "KHz" when appropriate. final StringBuffer b = new StringBuffer(); b.append(encoding); // b.append(", "); if (FormatUtils.specified(sampleRate)) { // FIXME: This removes .0 b.append(", " + (int) sampleRate); if (!asInput) { b.append(" Hz"); } } else { if (!asInput) { b.append(", Unknown Sample Rate"); } } if (FormatUtils.specified(sampleSizeInBits)) { b.append(", "); b.append("" + sampleSizeInBits); if (!asInput) { b.append("-bit"); } } if (FormatUtils.specified(channels)) { b.append(", "); if (!asInput) { if (channels == 1) { b.append("Mono"); } else if (channels == 2) { b.append("Stereo"); } else { b.append("" + channels + "-channel"); } } else { b.append("" + channels); } } if (FormatUtils.specified(endian) && FormatUtils.specified(sampleSizeInBits) && sampleSizeInBits > 8) { b.append(", "); if (!asInput) { if (endian == BIG_ENDIAN) { b.append("BigEndian"); } else if (endian == LITTLE_ENDIAN) { b.append("LittleEndian"); } else { // unknown, don't append anything } } else { b.append("" + endian); } } if (FormatUtils.specified(signed)) { b.append(", "); if (!asInput) { if (signed != SIGNED) { b.append("Unsigned"); } else { b.append("Signed"); } } else { b.append("" + signed); } } if (FormatUtils.specified(frameRate)) { b.append(", "); if (!asInput) { b.append("" + frameRate + " frame rate"); } else { b.append("" + frameRate); } } if (FormatUtils.specified(frameSizeInBits)) { if (!asInput) { b.append(", FrameSize=" + frameSizeInBits + " bits"); } else { b.append(", " + frameSizeInBits); } } return b.toString(); }
/** Tests the appropriate method. */ @Test public void testTimeString2TotalSeconds() { FormatUtils formatUtils = new FormatUtils(FormatUtils.UnitSystem.Metric, FormatUtils.SpeedView.DistancePerHour); assertEquals(formatUtils.timeString2TotalSeconds(null), -1); assertEquals(formatUtils.timeString2TotalSeconds(""), -1); assertEquals(formatUtils.timeString2TotalSeconds("affe"), -1); assertEquals(formatUtils.timeString2TotalSeconds(":"), -1); assertEquals(formatUtils.timeString2TotalSeconds("::"), -1); assertEquals(formatUtils.timeString2TotalSeconds("12:12:12:12"), -1); assertEquals(formatUtils.timeString2TotalSeconds("12:12:60"), -1); assertEquals(formatUtils.timeString2TotalSeconds("12:60:12"), -1); assertEquals(formatUtils.timeString2TotalSeconds("-12:12:12"), -1); assertEquals(formatUtils.timeString2TotalSeconds("12:-12:12"), -1); assertEquals(formatUtils.timeString2TotalSeconds("12:12:-12"), -1); assertEquals(formatUtils.timeString2TotalSeconds("a:b:c"), -1); assertEquals(formatUtils.timeString2TotalSeconds("12a:12:12"), -1); assertEquals(formatUtils.timeString2TotalSeconds("12:12b:12"), -1); assertEquals(formatUtils.timeString2TotalSeconds("12:12:12c"), -1); assertEquals(formatUtils.timeString2TotalSeconds(":12:12"), -1); assertEquals(formatUtils.timeString2TotalSeconds("12::12"), -1); assertEquals(formatUtils.timeString2TotalSeconds("0"), 0); assertEquals(formatUtils.timeString2TotalSeconds("5"), 5); assertEquals(formatUtils.timeString2TotalSeconds("37"), 37); assertEquals(formatUtils.timeString2TotalSeconds("3:0"), (3 * 60) + 0); assertEquals(formatUtils.timeString2TotalSeconds("3:5"), (3 * 60) + 5); assertEquals(formatUtils.timeString2TotalSeconds("32:48"), (32 * 60) + 48); assertEquals(formatUtils.timeString2TotalSeconds("3:0:0"), (3 * 60 * 60) + (0 * 60) + 0); assertEquals(formatUtils.timeString2TotalSeconds("13:5:34"), (13 * 60 * 60) + (5 * 60) + 34); assertEquals( formatUtils.timeString2TotalSeconds("114:32:48"), (114 * 60 * 60) + (32 * 60) + 48); }
@CliCommand(value = "describe cube", help = "describe cube") public String describeCube( @CliOption( key = {"", "cube"}, mandatory = true, help = "<cube-name>") String cubeName) { XCube cube = client.getCube(cubeName); StringBuilder builder = new StringBuilder(); builder.append("Cube Name : ").append(cube.getName()).append("\n"); builder .append("Description : ") .append(cube.getDescription() != null ? cube.getDescription() : ""); if (cube.getMeasures() != null) { builder.append("Measures :").append("\n"); builder .append("\t") .append("name") .append("\t") .append("type") .append("\t") .append("cost") .append("\t") .append("format string") .append("\t") .append("unit") .append("\t") .append("starttime(in miliseconds)") .append("\t") .append("endtime(in miliseconds)") .append("\n"); for (XMeasure measure : cube.getMeasures().getMeasures()) { builder .append("\t") .append(measure.getName() != null ? measure.getName() : "") .append("\t") .append(measure.getType() != null ? measure.getType() : "") .append("\t") .append(measure.getCost()) .append("\t") .append(measure.getFormatString() != null ? measure.getFormatString() : "") .append("\t") .append(measure.getUnit() != null ? measure.getUnit() : "") .append("\t") .append( measure.getStartTime() != null ? measure.getStartTime().toGregorianCalendar().getTimeInMillis() : "") .append("\t") .append( measure.getEndTime() != null ? measure.getEndTime().toGregorianCalendar().getTimeInMillis() : "") .append("\t") .append("\n"); } } if (cube.getDimensions() != null) { builder.append("Dimensions :").append("\n"); builder .append("\t") .append("name") .append("\t") .append("type") .append("\t") .append("cost") .append("\t") .append("Expression") .append("\t") .append("table references") .append("\t") .append("starttime(in miliseconds)") .append("\t") .append("endtime(in miliseconds)") .append("\n"); for (XDimension dim : cube.getDimensions().getDimensions()) { builder .append("\t") .append(dim.getName() != null ? dim.getName() : "") .append("\t") .append(dim.getType() != null ? dim.getType() : "") .append("\t") .append(dim.getCost() != null ? dim.getCost() : "") .append("\t") .append(dim.getExpr() != null ? dim.getExpr() : "") .append("\t") .append( dim.getReferences() != null ? getXtableString(dim.getReferences().getTableReferences()) : "") .append( dim.getStartTime() != null ? dim.getStartTime().toGregorianCalendar().getTimeInMillis() : "") .append("\t") .append( dim.getEndTime() != null ? dim.getEndTime().toGregorianCalendar().getTimeInMillis() : "") .append("\t") .append("\n"); } } builder.append(FormatUtils.formatProperties(cube.getProperties().getProperties())); return builder.toString(); }