You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2012/10/30 16:41:58 UTC
svn commit: r1403742 - in
/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure:
AngleFormat.java FormatField.java
Author: desruisseaux
Date: Tue Oct 30 15:41:57 2012
New Revision: 1403742
URL: http://svn.apache.org/viewvc?rev=1403742&view=rev
Log:
Replaced the static int *_FIELD constant by the Format.Field, to be needed
later for implementation of AngleFormat.formatToCharacterIterator(Object).
Added:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/FormatField.java (with props)
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java
Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java?rev=1403742&r1=1403741&r2=1403742&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java Tue Oct 30 15:41:57 2012
@@ -127,22 +127,28 @@ public class AngleFormat extends Format
* Constant for degrees field. When formatting a string, this value may be specified to the
* {@link FieldPosition} constructor in order to get the bounding index where degrees have
* been written.
+ *
+ * @see Field#DEGREES
*/
- public static final int DEGREES_FIELD = 0;
+ static final int DEGREES_FIELD = 0;
/**
* Constant for minutes field. When formatting a string, this value may be specified to the
* {@link FieldPosition} constructor in order to get the bounding index where minutes have
* been written.
+ *
+ * @see Field#MINUTES
*/
- public static final int MINUTES_FIELD = 1;
+ static final int MINUTES_FIELD = 1;
/**
* Constant for seconds field. When formatting a string, this value may be specified to the
* {@link FieldPosition} constructor in order to get the bounding index where seconds have
* been written.
+ *
+ * @see Field#SECONDS
*/
- public static final int SECONDS_FIELD = 2;
+ static final int SECONDS_FIELD = 2;
/**
* Constant for the fractional part of the degrees, minutes or seconds field. When formatting
@@ -155,8 +161,10 @@ public class AngleFormat extends Format
* Constant for hemisphere field. When formatting a string, this value may be specified to the
* {@link FieldPosition} constructor in order to get the bounding index where the hemisphere
* symbol has been written.
+ *
+ * @see Field#HEMISPHERE
*/
- public static final int HEMISPHERE_FIELD = 4;
+ static final int HEMISPHERE_FIELD = 4;
/**
* Symbols for degrees (0), minutes (1), seconds (2) and optional fraction digits (3).
@@ -165,6 +173,58 @@ public class AngleFormat extends Format
private static final char[] SYMBOLS = {'D', 'M', 'S', '#'};
/**
+ * Defines constants that are used as attribute keys in the iterator returned from
+ * {@link AngleFormat#formatToCharacterIterator(Object)}.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.3
+ * @version 0.3
+ * @module
+ */
+ public static final class Field extends FormatField {
+ /**
+ * For cross-version compatibility.
+ */
+ private static final long serialVersionUID = 3824094360855371451L;
+
+ /**
+ * Creates a new field of the given name. The given name shall
+ * be identical to the name of the public static constant.
+ */
+ private Field(final String name, final int fieldID) {
+ super(name, fieldID);
+ }
+
+ /**
+ * Identifies the degrees field, including the degrees symbol (if any).
+ * When formatting a string, this value may be specified to the {@link FieldPosition}
+ * constructor in order to get the bounding index where degrees have been written.
+ */
+ public static final Field DEGREES = new Field("DEGREES", DEGREES_FIELD);
+
+ /**
+ * Identifies the minutes field, including the minutes symbol (if any).
+ * When formatting a string, this value may be specified to the {@link FieldPosition}
+ * constructor in order to get the bounding index where minutes have been written.
+ */
+ public static final Field MINUTES = new Field("MINUTES", MINUTES_FIELD);
+
+ /**
+ * Identifies the seconds field, including the seconds symbol (if any).
+ * When formatting a string, this value may be specified to the {@link FieldPosition}
+ * constructor in order to get the bounding index where seconds have been written.
+ */
+ public static final Field SECONDS = new Field("SECONDS", SECONDS_FIELD);
+
+ /**
+ * Identifies the hemisphere symbol (if any).
+ * When formatting a string, this value may be specified to the {@link FieldPosition}
+ * constructor in order to get the bounding index where hemisphere have been written.
+ */
+ public static final Field HEMISPHERE = new Field("HEMISPHERE", HEMISPHERE_FIELD);
+ }
+
+ /**
* The locale specified at construction time.
*/
private final Locale locale;
@@ -240,7 +300,7 @@ public class AngleFormat extends Format
*/
private FieldPosition dummyFieldPosition() {
if (dummy == null) {
- dummy = new FieldPosition(0);
+ dummy = new FieldPosition(-1);
}
return dummy;
}
@@ -676,6 +736,20 @@ scan: for (int i=0; i<length;) {
}
/**
+ * Returns the {@code *_FIELD} constant for the given field position, or -1 if none.
+ */
+ private static int getField(final FieldPosition position) {
+ if (position != null) {
+ final Format.Field field = position.getFieldAttribute();
+ if (field instanceof Field) {
+ return ((Field) field).field;
+ }
+ return position.getField();
+ }
+ return -1;
+ }
+
+ /**
* Formats an angle. The angle will be formatted according the pattern given to the last call
* of {@link #applyPattern(String)}.
*
@@ -697,15 +771,20 @@ scan: for (int i=0; i<length;) {
* @param pos
* An optional object where to store the position of the field in the formatted
* text, or {@code null} if this information is not wanted. This field position
- * shall be created with one of the following constants: {@link #DEGREES_FIELD},
- * {@link #MINUTES_FIELD}, {@link #SECONDS_FIELD} or {@link #HEMISPHERE_FIELD}.
+ * shall be created with one of the {@link Field} constants.
*
* @return The {@code toAppendTo} buffer, returned for method calls chaining.
*/
public StringBuffer format(double angle, StringBuffer toAppendTo, final FieldPosition pos) {
+ final int offset = toAppendTo.length();
+ final int fieldPos = getField(pos);
if (isNaN(angle) || isInfinite(angle)) {
- return numberFormat().format(angle, toAppendTo,
- (pos != null) ? pos : new FieldPosition(DecimalFormat.INTEGER_FIELD));
+ toAppendTo = numberFormat().format(angle, toAppendTo, dummyFieldPosition());
+ if (fieldPos >= DEGREES_FIELD && fieldPos <= SECONDS_FIELD) {
+ pos.setBeginIndex(offset);
+ pos.setEndIndex(toAppendTo.length());
+ }
+ return toAppendTo;
}
double degrees = angle;
/*
@@ -753,14 +832,7 @@ scan: for (int i=0; i<length;) {
/*
* Formats fields in a loop from DEGREES_FIELD to SECONDS_FIELD inclusive.
*/
- int field = DEGREES_FIELD;
- int fieldPos = PREFIX_FIELD; // Dummy value not in the DEGREES ⦠SECONDS_FIELD range.
- if (pos != null) {
- fieldPos = pos.getField();
- pos.setBeginIndex(0);
- pos.setEndIndex(0);
- }
- final int offset = toAppendTo.length();
+ int field = DEGREES_FIELD;
if (prefix != null) {
toAppendTo.append(prefix);
}
@@ -845,8 +917,7 @@ scan: for (int i=0; i<length;) {
* @param pos
* An optional object where to store the position of the field in the formatted
* text, or {@code null} if this information is not wanted. This field position
- * shall be created with one of the following constants: {@link #DEGREES_FIELD},
- * {@link #MINUTES_FIELD}, {@link #SECONDS_FIELD} or {@link #HEMISPHERE_FIELD}.
+ * shall be created with one of the {@link Field} constants.
*
* @return The {@code toAppendTo} buffer, returned for method calls chaining.
* @throws IllegalArgumentException if {@code value} if not an instance of {@link Angle}.
@@ -881,7 +952,7 @@ scan: for (int i=0; i<length;) {
toAppendTo = format(abs(angle), toAppendTo, pos);
final int start = toAppendTo.length();
toAppendTo.append(isNegative(angle) ? negativeSuffix : positiveSuffix);
- if (pos != null && pos.getField() == HEMISPHERE_FIELD) {
+ if (getField(pos) == HEMISPHERE_FIELD) {
pos.setBeginIndex(start);
pos.setEndIndex(toAppendTo.length()-1);
}
Added: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/FormatField.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/FormatField.java?rev=1403742&view=auto
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/FormatField.java (added)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/FormatField.java Tue Oct 30 15:41:57 2012
@@ -0,0 +1,72 @@
+/*
+ * 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.sis.measure;
+
+import java.io.InvalidObjectException;
+import java.text.Format;
+
+
+/**
+ * Base class of format fields.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.3
+ * @version 0.3
+ * @module
+ */
+abstract class FormatField extends Format.Field {
+ /**
+ * Serial number for cross-version compatibility.
+ */
+ private static final long serialVersionUID = -5344437554919766622L;
+
+ /**
+ * The numeric {@code *_FIELD} value for this constant. This value doesn't need to
+ * be serialized, because {@link #readResolve()} will locate the original constant
+ * on deserialization.
+ */
+ final transient int field;
+
+ /**
+ * Creates a new field of the given name.
+ *
+ * @param name The name, which shall be identical to the name of the public static constant.
+ * @param field The numeric identifier of this field.
+ */
+ FormatField(final String name, final int field) {
+ super(name);
+ this.field = field;
+ }
+
+ /**
+ * Invoked on deserialization for resolving this instance to one of the predefined constants.
+ *
+ * @return One of the predefined constants.
+ * @throws InvalidObjectException If this instance can not be resolved.
+ */
+ @Override
+ protected final Object readResolve() throws InvalidObjectException {
+ final Class<?> type = getClass();
+ try {
+ return type.cast(type.getField(getName()).get(null));
+ } catch (Exception cause) { // Many exceptions, including unchecked ones.
+ InvalidObjectException e = new InvalidObjectException(cause.toString());
+ e.initCause(cause);
+ throw e;
+ }
+ }
+}
Propchange: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/FormatField.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/FormatField.java
------------------------------------------------------------------------------
svn:mime-type = text/plain