oscim.core.Tag: cleanups

- consistent String constants
This commit is contained in:
Hannes Janetzek 2013-09-25 05:50:54 +02:00
parent 2db1cf8b6c
commit c05710fcdb
9 changed files with 71 additions and 82 deletions

View File

@ -68,8 +68,8 @@ public class VectorTileLoader extends TileLoader implements IRenderTheme.Callbac
// replacement for variable value tags that should not be matched by RenderTheme
// FIXME make this general, maybe subclass tags
private static final Tag mTagEmptyName = new Tag(Tag.TAG_KEY_NAME, null, false);
private static final Tag mTagEmptyHouseNr = new Tag(Tag.TAG_KEY_HOUSE_NUMBER, null, false);
private static final Tag mTagEmptyName = new Tag(Tag.KEY_NAME, null, false);
private static final Tag mTagEmptyHouseNr = new Tag(Tag.KEY_HOUSE_NUMBER, null, false);
private IRenderTheme renderTheme;
private int renderLevels;
@ -208,12 +208,12 @@ public class VectorTileLoader extends TileLoader implements IRenderTheme.Callbac
for (int i = 0; i < in.numTags; i++) {
String key = tags[i].key;
if (key == Tag.TAG_KEY_NAME) {
if (key == Tag.KEY_NAME) {
if (tags[i].value != null) {
mTagName = tags[i];
tags[i] = mTagEmptyName;
}
} else if (key == Tag.TAG_KEY_HOUSE_NUMBER) {
} else if (key == Tag.KEY_HOUSE_NUMBER) {
if (tags[i].value != null) {
mTagHouseNr = tags[i];
tags[i] = mTagEmptyHouseNr;
@ -221,7 +221,7 @@ public class VectorTileLoader extends TileLoader implements IRenderTheme.Callbac
} else if (mTile.zoomLevel > 16) {
// FIXME, allow overlays to intercept
// this, or use a theme option for this
if (key == Tag.TAG_KEY_BUILDING)
if (key == Tag.KEY_BUILDING)
mRenderBuildingModel = true;
else if (key == Tag.KEY_HEIGHT) {
try {
@ -381,10 +381,10 @@ public class VectorTileLoader extends TileLoader implements IRenderTheme.Callbac
private String textValueForKey(Text text) {
String value = null;
if (text.textKey == Tag.TAG_KEY_NAME) {
if (text.textKey == Tag.KEY_NAME) {
if (mTagName != null)
value = mTagName.value;
} else if (text.textKey == Tag.TAG_KEY_HOUSE_NUMBER) {
} else if (text.textKey == Tag.KEY_HOUSE_NUMBER) {
if (mTagHouseNr != null)
value = mTagHouseNr.value;
}

View File

@ -1,5 +1,6 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
* Copyright 2013 Hannes Janetzek
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
@ -15,47 +16,32 @@
package org.oscim.core;
/**
* A tag represents an immutable key-value pair.
* A tag represents an immutable key-value pair. Keys are always intern().
*/
// TODO: use own stringshare method instead of internalized strings
public class Tag {
private static final char KEY_VALUE_SEPARATOR = '=';
/**
* The key of the house number OpenStreetMap tag.
*/
public static final String TAG_KEY_HOUSE_NUMBER = "addr:housenumber";
/** The key of the house number OpenStreetMap tag. */
public static final String KEY_HOUSE_NUMBER = "addr:housenumber";
/**
* The key of the name OpenStreetMap tag.
*/
public static final String TAG_KEY_NAME = "name";
/** The key of the name OpenStreetMap tag. */
public static final String KEY_NAME = "name";
/**
* The key of the reference OpenStreetMap tag.
*/
public static final String TAG_KEY_REF = "ref";
/** The key of the reference OpenStreetMap tag. */
public static final String KEY_REF = "ref";
/**
* The key of the elevation OpenStreetMap tag.
*/
public static final String TAG_KEY_ELE = "ele";
public static final String TAG_KEY_AMENITY = "amenity";
/**
* The key of the elevation OpenStreetMap tag.
*/
public static final String TAG_KEY_BUILDING = "building";
public static final String TAG_KEY_HIGHWAY = "highway";
public static final String TAG_KEY_LANDUSE = "landuse";
public static final String VALUE_YES = "yes";
public static final String VALUE_NO = "no";
/** The key of the elevation OpenStreetMap tag. */
public static final String KEY_ELE = "ele";
public static final String KEY_AMENITY = "amenity";
public static final String KEY_BUILDING = "building";
public static final String KEY_HIGHWAY = "highway";
public static final String KEY_LANDUSE = "landuse";
public static final String KEY_HEIGHT = "height";
public static final String KEY_MIN_HEIGHT = "min_height";
public static final String VALUE_YES = "yes";
public static final String VALUE_NO = "no";
/**
* The key of this tag.
*/
@ -66,23 +52,12 @@ public class Tag {
*/
public String value;
private int hashCodeValue = 0;
/**
* true when value is intern().
*/
private final boolean intern;
/**
* @param tag
* the textual representation of the tag.
*/
public Tag(String tag) {
int splitPosition = tag.indexOf(KEY_VALUE_SEPARATOR);
if (splitPosition < 0) {
System.out.println("TAG:" + tag);
}
this.key = tag.substring(0, splitPosition).intern();
this.value = tag.substring(splitPosition + 1).intern();
this.intern = true;
}
private int hashCodeValue = 0;
/**
* @param key
@ -165,4 +140,17 @@ public class Tag {
result = 31 * result + ((this.value == null) ? 0 : this.value.hashCode());
return result;
}
/**
* @param tag
* the textual representation of the tag.
*/
public static Tag parse(String tag) {
int splitPosition = tag.indexOf("=");
if (splitPosition < 0) {
return new Tag(tag, "");
}
return new Tag(tag.substring(0, splitPosition), tag.substring(splitPosition + 1));
}
}

View File

@ -61,8 +61,8 @@ public class VectorTileLoader extends TileLoader implements IRenderTheme.Callbac
// replacement for variable value tags that should not be matched by RenderTheme
// FIXME make this general, maybe subclass tags
private static final Tag mTagEmptyName = new Tag(Tag.TAG_KEY_NAME, null, false);
private static final Tag mTagEmptyHouseNr = new Tag(Tag.TAG_KEY_HOUSE_NUMBER, null, false);
private static final Tag mTagEmptyName = new Tag(Tag.KEY_NAME, null, false);
private static final Tag mTagEmptyHouseNr = new Tag(Tag.KEY_HOUSE_NUMBER, null, false);
// private final MapElement mDebugWay, mDebugPoint;
@ -187,12 +187,12 @@ public class VectorTileLoader extends TileLoader implements IRenderTheme.Callbac
for (int i = 0; i < in.numTags; i++) {
String key = tags[i].key;
if (tags[i].key == Tag.TAG_KEY_NAME) {
if (tags[i].key == Tag.KEY_NAME) {
if (tags[i].value != null) {
mTagName = tags[i];
tags[i] = mTagEmptyName;
}
} else if (tags[i].key == Tag.TAG_KEY_HOUSE_NUMBER) {
} else if (tags[i].key == Tag.KEY_HOUSE_NUMBER) {
if (tags[i].value != null) {
mTagHouseNr = tags[i];
tags[i] = mTagEmptyHouseNr;
@ -200,7 +200,7 @@ public class VectorTileLoader extends TileLoader implements IRenderTheme.Callbac
} else if (mTile.zoomLevel > 16) {
// FIXME, allow overlays to intercept
// this, or use a theme option for this
if (key == Tag.TAG_KEY_BUILDING)
if (key == Tag.KEY_BUILDING)
mRenderBuildingModel = true;
else if (key == Tag.KEY_HEIGHT) {
@ -369,10 +369,10 @@ public class VectorTileLoader extends TileLoader implements IRenderTheme.Callbac
private String textValueForKey(Text text) {
String value = null;
if (text.textKey == Tag.TAG_KEY_NAME) {
if (text.textKey == Tag.KEY_NAME) {
if (mTagName != null)
value = mTagName.value;
} else if (text.textKey == Tag.TAG_KEY_HOUSE_NUMBER) {
} else if (text.textKey == Tag.KEY_HOUSE_NUMBER) {
if (mTagHouseNr != null)
value = mTagHouseNr.value;
}

View File

@ -503,7 +503,7 @@ public class MapDatabase implements ITileDataSource {
// check if the POI has a name
if ((featureByte & POI_FEATURE_NAME) != 0) {
String str = mReadBuffer.readUTF8EncodedString();
mElem.tags.add(new Tag(Tag.TAG_KEY_NAME, str, false));
mElem.tags.add(new Tag(Tag.KEY_NAME, str, false));
}
// check if the POI has a house number
@ -801,30 +801,30 @@ public class MapDatabase implements ITileDataSource {
if (hasName) {
int textPos = mReadBuffer.readUnsignedInt();
String str = mReadBuffer.readUTF8EncodedStringAt(stringOffset + textPos);
mElem.tags.add(new Tag(Tag.TAG_KEY_NAME, str, false));
mElem.tags.add(new Tag(Tag.KEY_NAME, str, false));
}
if (hasHouseNr) {
int textPos = mReadBuffer.readUnsignedInt();
String str = mReadBuffer.readUTF8EncodedStringAt(stringOffset + textPos);
mElem.tags.add(new Tag(Tag.TAG_KEY_HOUSE_NUMBER, str, false));
mElem.tags.add(new Tag(Tag.KEY_HOUSE_NUMBER, str, false));
}
if (hasRef) {
int textPos = mReadBuffer.readUnsignedInt();
String str = mReadBuffer.readUTF8EncodedStringAt(stringOffset + textPos);
mElem.tags.add(new Tag(Tag.TAG_KEY_REF, str, false));
mElem.tags.add(new Tag(Tag.KEY_REF, str, false));
}
} else {
if (hasName) {
String str = mReadBuffer.readUTF8EncodedString();
mElem.tags.add(new Tag(Tag.TAG_KEY_NAME, str, false));
mElem.tags.add(new Tag(Tag.KEY_NAME, str, false));
}
if (hasHouseNr) {
String str = mReadBuffer.readUTF8EncodedString();
mElem.tags.add(new Tag(Tag.TAG_KEY_HOUSE_NUMBER, str, false));
mElem.tags.add(new Tag(Tag.KEY_HOUSE_NUMBER, str, false));
}
if (hasRef) {
String str = mReadBuffer.readUTF8EncodedString();
mElem.tags.add(new Tag(Tag.TAG_KEY_REF, str, false));
mElem.tags.add(new Tag(Tag.KEY_REF, str, false));
}
}
if ((featureByte & WAY_FEATURE_LABEL_POSITION) != 0)

View File

@ -171,7 +171,7 @@ final class RequiredFields {
if (tag == null) {
return new OpenResult("POI tag must not be null: " + currentTagId);
}
poiTags[currentTagId] = new Tag(tag);
poiTags[currentTagId] = Tag.parse(tag);
}
mapFileInfoBuilder.poiTags = poiTags;
return OpenResult.SUCCESS;
@ -227,7 +227,7 @@ final class RequiredFields {
if (tag == null) {
return new OpenResult("way tag must not be null: " + currentTagId);
}
wayTags[currentTagId] = new Tag(tag);
wayTags[currentTagId] = Tag.parse(tag);
}
mapFileInfoBuilder.wayTags = wayTags;
return OpenResult.SUCCESS;

View File

@ -175,7 +175,7 @@ public class TileDecoder extends PbfDecoder {
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
if (!key.startsWith(Tag.TAG_KEY_NAME))
if (!key.startsWith(Tag.KEY_NAME))
continue;
int len = key.length();
if (len == 4) {
@ -222,7 +222,7 @@ public class TileDecoder extends PbfDecoder {
if (keyIdx == matchedLocal) {
hasName = true;
f.elem.tags.add(new Tag(Tag.TAG_KEY_NAME, val, false));
f.elem.tags.add(new Tag(Tag.KEY_NAME, val, false));
} else {
key = keys.get(keyIdx);
@ -231,7 +231,7 @@ public class TileDecoder extends PbfDecoder {
}
if (!hasName && fallbackName != null)
f.elem.tags.add(new Tag(Tag.TAG_KEY_NAME, fallbackName, false));
f.elem.tags.add(new Tag(Tag.KEY_NAME, fallbackName, false));
// FIXME extract layer tag here
f.elem.setLayer(5);

View File

@ -130,18 +130,19 @@ public class TileDecoder extends PbfDecoder {
String tagString = decodeString();
if (tagString == null || tagString.length() == 0) {
curTags[mCurTagCnt++] = new Tag(Tag.TAG_KEY_NAME, "...");
curTags[mCurTagCnt++] = new Tag(Tag.KEY_NAME, "...");
return false;
}
Tag tag = tagHash.get(tagString);
if (tag == null) {
if (tagString.startsWith(Tag.TAG_KEY_NAME))
tag = new Tag(Tag.TAG_KEY_NAME, tagString.substring(5), false);
if (tagString.startsWith(Tag.KEY_NAME))
tag = new Tag(Tag.KEY_NAME, tagString.substring(5), false);
else
tag = new Tag(tagString);
tag = Tag.parse(tagString);
if (tag != null)
tagHash.put(tagString, tag);
}

View File

@ -155,7 +155,7 @@ public class OSciMap2TileSource extends UrlTileSource {
String key = Tags.keys[mSArray[curTag]];
Tag tag;
if (key == Tag.TAG_KEY_NAME)
if (key == Tag.KEY_NAME)
tag = new Tag(key, tagString, false);
else
tag = new Tag(key, tagString, true);

View File

@ -209,12 +209,12 @@ public class TileDecoder extends PbfDecoder {
// FIXME filter out all variable tags
// might depend on theme though
if (key == Tag.TAG_KEY_NAME
if (key == Tag.KEY_NAME
|| key == Tag.KEY_HEIGHT
|| key == Tag.KEY_MIN_HEIGHT
|| key == Tag.TAG_KEY_HOUSE_NUMBER
|| key == Tag.TAG_KEY_REF
|| key == Tag.TAG_KEY_ELE)
|| key == Tag.KEY_HOUSE_NUMBER
|| key == Tag.KEY_REF
|| key == Tag.KEY_ELE)
tag = new Tag(key, val, false);
else
tag = new Tag(key, val, true);