source: josm/trunk/src/org/openstreetmap/josm/data/osm/OsmUtils.java@ 9853

Last change on this file since 9853 was 8846, checked in by Don-vip, 9 years ago

sonar - fb-contrib - minor performance improvements:

  • Method passes constant String of length 1 to character overridden method
  • Method needlessly boxes a boolean constant
  • Method uses iterator().next() on a List to get the first item
  • Method converts String to boxed primitive using excessive boxing
  • Method converts String to primitive using excessive boxing
  • Method creates array using constants
  • Class defines List based fields but uses them like Sets
  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.util.Arrays;
5import java.util.HashSet;
6import java.util.Locale;
7import java.util.Map;
8import java.util.Set;
9
10import org.openstreetmap.josm.data.coor.LatLon;
11import org.openstreetmap.josm.tools.CheckParameterUtil;
12import org.openstreetmap.josm.tools.TextTagParser;
13
14public final class OsmUtils {
15
16 private static final Set<String> TRUE_VALUES = new HashSet<>(Arrays
17 .asList("true", "yes", "1", "on"));
18 private static final Set<String> FALSE_VALUES = new HashSet<>(Arrays
19 .asList("false", "no", "0", "off"));
20 private static final Set<String> REVERSE_VALUES = new HashSet<>(Arrays
21 .asList("reverse", "-1"));
22
23 public static final String trueval = "yes";
24 public static final String falseval = "no";
25 public static final String reverseval = "-1";
26
27 private OsmUtils() {
28 // Hide default constructor for utils classes
29 }
30
31 public static Boolean getOsmBoolean(String value) {
32 if (value == null) return null;
33 String lowerValue = value.toLowerCase(Locale.ENGLISH);
34 if (TRUE_VALUES.contains(lowerValue)) return Boolean.TRUE;
35 if (FALSE_VALUES.contains(lowerValue)) return Boolean.FALSE;
36 return null;
37 }
38
39 public static String getNamedOsmBoolean(String value) {
40 Boolean res = getOsmBoolean(value);
41 return res == null ? value : (res ? trueval : falseval);
42 }
43
44 public static boolean isReversed(String value) {
45 return REVERSE_VALUES.contains(value);
46 }
47
48 public static boolean isTrue(String value) {
49 return TRUE_VALUES.contains(value);
50 }
51
52 public static boolean isFalse(String value) {
53 return FALSE_VALUES.contains(value);
54 }
55
56 /**
57 * Creates a new OSM primitive according to the given assertion. Originally written for unit tests,
58 * this can also be used in another places like validation of local MapCSS validator rules.
59 * @param assertion The assertion describing OSM primitive (ex: "way name=Foo railway=rail")
60 * @return a new OSM primitive according to the given assertion
61 * @throws IllegalArgumentException if assertion is null or if the primitive type cannot be deduced from it
62 * @since 7356
63 */
64 public static OsmPrimitive createPrimitive(String assertion) {
65 CheckParameterUtil.ensureParameterNotNull(assertion, "assertion");
66 final String[] x = assertion.split("\\s+", 2);
67 final OsmPrimitive p = "n".equals(x[0]) || "node".equals(x[0])
68 ? new Node(LatLon.ZERO)
69 : "w".equals(x[0]) || "way".equals(x[0]) || /*for MapCSS related usage*/ "area".equals(x[0])
70 ? new Way()
71 : "r".equals(x[0]) || "relation".equals(x[0])
72 ? new Relation()
73 : null;
74 if (p == null) {
75 throw new IllegalArgumentException("Expecting n/node/w/way/r/relation/area, but got '" + x[0] + '\'');
76 }
77 for (final Map.Entry<String, String> i : TextTagParser.readTagsFromText(x[1]).entrySet()) {
78 p.put(i.getKey(), i.getValue());
79 }
80 return p;
81 }
82}
Note: See TracBrowser for help on using the repository browser.