Skip to content

Commit

Permalink
[attributes] Cleaning code from PR #138
Browse files Browse the repository at this point in the history
amend #138

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Jun 19, 2016
1 parent 6b9d34c commit f77896e
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -151,55 +148,43 @@ public enum AttributeType {

private static final String NAME_RESOURCE_FILE;

private String localizedName;

static {
final String pName = AttributeType.class.getPackage().getName();
NAME_RESOURCE_FILE = pName + ".types"; //$NON-NLS-1$
}

/** FactoryMap collection.
/** Replies the name of this type (localized).
*
* @return the localized name of this type.
*/
private static final Map<AttributeType, StringFactory> FACTORY_MAP =
Collections.unmodifiableMap(new HashMap<AttributeType, StringFactory>() {{
put(AttributeType.INTEGER, () -> Locale.getStringFrom(NAME_RESOURCE_FILE, "INTEGER")); //$NON-NLS-1$
put(AttributeType.REAL, () -> Locale.getStringFrom(NAME_RESOURCE_FILE, "FLOAT")); //$NON-NLS-1$
put(AttributeType.STRING, () -> Locale.getStringFrom(NAME_RESOURCE_FILE, "STRING")); //$NON-NLS-1$
put(AttributeType.BOOLEAN, () -> Locale.getStringFrom(NAME_RESOURCE_FILE, "BOOLEAN")); //$NON-NLS-1$
put(AttributeType.DATE, () -> Locale.getStringFrom(NAME_RESOURCE_FILE, "DATE")); //$NON-NLS-1$
put(AttributeType.OBJECT, () -> Locale.getStringFrom(NAME_RESOURCE_FILE, "OBJECT")); //$NON-NLS-1$
put(AttributeType.TIMESTAMP, () -> Locale.getStringFrom(NAME_RESOURCE_FILE, "TIMESTAMP")); //$NON-NLS-1$
put(AttributeType.POINT3D, () -> Locale.getStringFrom(NAME_RESOURCE_FILE, "POINT3D")); //$NON-NLS-1$
put(AttributeType.POINT, () -> Locale.getStringFrom(NAME_RESOURCE_FILE, "POINT2D")); //$NON-NLS-1$
put(AttributeType.COLOR, () -> Locale.getStringFrom(NAME_RESOURCE_FILE, "COLOR")); //$NON-NLS-1$
put(AttributeType.IMAGE, () -> Locale.getStringFrom(NAME_RESOURCE_FILE, "ICON")); //$NON-NLS-1$
put(AttributeType.POLYLINE, () -> Locale.getStringFrom(NAME_RESOURCE_FILE, "POLYLINE")); //$NON-NLS-1$
put(AttributeType.POLYLINE3D, () -> Locale.getStringFrom(NAME_RESOURCE_FILE, "POLYLINE3D")); //$NON-NLS-1$
put(AttributeType.UUID, () -> Locale.getStringFrom(NAME_RESOURCE_FILE, "UUID")); //$NON-NLS-1$
put(AttributeType.URL, () -> Locale.getStringFrom(NAME_RESOURCE_FILE, "URL")); //$NON-NLS-1$
put(AttributeType.URI, () -> Locale.getStringFrom(NAME_RESOURCE_FILE, "URI")); //$NON-NLS-1$
put(AttributeType.INET_ADDRESS, () -> Locale.getStringFrom(NAME_RESOURCE_FILE, "INET_ADDRESS")); //$NON-NLS-1$
put(AttributeType.ENUMERATION, () -> Locale.getStringFrom(NAME_RESOURCE_FILE, "ENUMERATION")); //$NON-NLS-1$
put(AttributeType.TYPE, () -> Locale.getStringFrom(NAME_RESOURCE_FILE, "TYPE")); //$NON-NLS-1$
}});
@Pure
public String getLocalizedName() {
if (this.localizedName == null) {
this.localizedName = Locale.getStringWithDefaultFrom(NAME_RESOURCE_FILE, name(), null);
if (this.localizedName == null) {
this.localizedName = Locale.getStringFrom(NAME_RESOURCE_FILE, "OTHER"); //$NON-NLS-1$
}
}
return this.localizedName;
}

/** Replies the name of this type (localized).
*
* @return the localized name of this type.
* @deprecated since 13.0, see {@link #getLocalizedName()}
*/
@Pure
@SuppressWarnings({"checkstyle:returncount"})
@Deprecated
public String getName() {
final StringFactory stringFactory = FACTORY_MAP.get(this);
if (stringFactory != null) {
return stringFactory.createString();
}
return Locale.getStringFrom(NAME_RESOURCE_FILE, "OTHER"); //$NON-NLS-1$
return getLocalizedName();
}

@Pure
@Override
public String toString() {
return getName();
return getLocalizedName();
}

/** Replies the Attribute type that corresponds to the
Expand Down Expand Up @@ -934,12 +919,4 @@ private static InetAddress getInetAddressFromCharacterSequence(Object obj) {
return Point3D[].class.cast(obj);
}

/** Represents a Java type.
* @mavenartifactid $ArtifactId$
* @mavengroupid $GroupId$
*/
@FunctionalInterface
private interface StringFactory {
String createString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ BOOLEAN = Boolean
COLOR = Color
DATE = Date
ENUMERATION = Enumeration
FLOAT = Float
REAL = Floating point number
ICON = Icon
INET_ADDRESS = Internet Address
INTEGER = Integer
OBJECT = Object
OTHER = Other
POINT2D = Point 2d
POINT = Point 2d
POINT3D = Point 3d
POLYLINE = Polyline 2d
POLYLINE3D = Polyline 3d
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ BOOLEAN = Bool
COLOR = Couleur
DATE = Date
ENUMERATION = Enumeration
FLOAT = Nombre décimal
REAL = Nombre décimal
ICON = Icône
INET_ADDRESS = Addresse Internet
INTEGER = Nombre entier
OBJECT = Objet
OTHER = Autre
POINT2D = Point 2d
POINT = Point 2d
POINT3D = Point 3d
POLYLINE = Poly-ligne 2d
POLYLINE3D = Poly-ligne 3d
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected static void assertNullException(AttributeType type, Object value) {
@Test
public void getName() {
for(AttributeType type : AttributeType.values()) {
String name = type.getName();
String name = type.getLocalizedName();
assertNotNull(name);
assertNotSame("", name); //$NON-NLS-1$
}
Expand Down Expand Up @@ -1168,4 +1168,12 @@ public void isAssignableFrom() {
assertTrue(AttributeType.ENUMERATION.isAssignableFrom(AttributeType.ENUMERATION));
}

@Test
public void getLocalizedName() {
for (AttributeType type : AttributeType.values()) {
String name = type.getLocalizedName();
assertNotEquals("Invalid attribute name", "OTHER", name); //$NON-NLS-1$ //$NON-NLS-2$
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3359,7 +3359,7 @@ public void parse() {
v = AttributeValueImpl.parse("java.lang.String"); //$NON-NLS-1$
assertSame(AttributeType.TYPE, v.getType());

v = AttributeValueImpl.parse(AttributeType.class.getName()+"."+AttributeType.ENUMERATION.toString()); //$NON-NLS-1$
v = AttributeValueImpl.parse(AttributeType.class.getName()+"."+AttributeType.ENUMERATION.name()); //$NON-NLS-1$
assertSame(AttributeType.ENUMERATION, v.getType());

v = AttributeValueImpl.parse("3eade434-b267-4ffa-a574-2e2cbff0151a"); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,30 @@ public static void assertNotEquals(String message, int expected, int actual) {
}
}

/** Test if the actual value is not equal to the expected value with
* a distance of epsilon.
*
* @param expected the expected value.
* @param actual the actual value.
*/
public static void assertNotEquals(Object expected, Object actual) {
assertNotEquals(null, expected, actual);
}

/** Test if the actual value is not equal to the expected value with
* a distance of epsilon.
*
* @param message the error message.
* @param expected the expected value.
* @param actual the actual value.
*/
public static void assertNotEquals(String message, Object expected, Object actual) {
if (!Objects.equals(expected, actual)) {
throw new junit.framework.ComparisonFailure(message,
Objects.toString(expected), Objects.toString(actual));
}
}

/** Test if the given value is not a number.
*
* @param value the value to test.
Expand Down

0 comments on commit f77896e

Please sign in to comment.