Skip to content

Commit

Permalink
Rename methods of map that uses optional values
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Mendez committed Jun 10, 2017
1 parent e29b38e commit 7359d1e
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,7 @@ public interface OptMap<K, V> {
* @throws NullPointerException
* if a <code>null</code> value is given
*/
boolean isKeyContained(K key);

// /**
// * @deprecated Replaced by {@link #isKeyContained isKeyContained(K)}
// */
// @Deprecated
// public boolean containsKey(Object key);
boolean containsKey(K key);

/**
* Returns <code>true</code> if and only if this map associates one or more
Expand All @@ -67,13 +61,7 @@ public interface OptMap<K, V> {
* @throws NullPointerException
* if a <code>null</code> value is given
*/
boolean isValueContained(V value);

// /**
// * @deprecated Replaced by {@link #isValueContained isValueContained(V)}
// */
// @Deprecated
// boolean containsValue(Object value);
boolean containsValue(V value);

/**
* Returns an optional containing the value associated to the given key, is
Expand All @@ -85,13 +73,7 @@ public interface OptMap<K, V> {
* @return an optional containing the value associated to the given key, is
* this association exists, or an empty optional otherwise
*/
Optional<V> getOpt(K key);

// /**
// * @deprecated Replaced by {@link #getOpt getOpt(K)}
// */
// @Deprecated
// V get(Object key);
Optional<V> get(K key);

/**
* Associates the given value with the given key. This method replaces
Expand All @@ -107,13 +89,7 @@ public interface OptMap<K, V> {
* @throws NullPointerException
* if a <code>null</code> value is given
*/
Optional<V> putOpt(K key, V value);

// /**
// * @deprecated Replaced by {@link #putOpt putOpt(K, V)}
// */
// @Deprecated
// V put(K key, V value);
Optional<V> put(K key, V value);

/**
* Removes the mapping for the given key. This method replaces
Expand All @@ -127,13 +103,7 @@ public interface OptMap<K, V> {
* @throws NullPointerException
* if a <code>null</code> value is given
*/
public Optional<V> removeOpt(K key);

// /**
// * @deprecated Replaced by {@link #removeOpt removeOpt(K)}
// */
// @Deprecated
// V remove(Object key);
public Optional<V> remove(K key);

/**
* Clears this map.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import java.util.Set;

/**
* This is the default implementation of {@link OptMap}.
* This is the default implementation of {@link OptMap}. This implementation
* does not copy the map passed as an argument to create the instance, and it
* uses the map itself instead. This means that the internal representation can
* be modified externally, or it can be retrieved by using {@link #asMap()}.
*
* @author Julian Mendez
*
Expand Down Expand Up @@ -62,72 +65,36 @@ public boolean isEmpty() {
}

@Override
public boolean isKeyContained(K key) {
public boolean containsKey(K key) {
Objects.requireNonNull(key);
return this.map.containsKey(key);
}

// @Override
// @Deprecated
// public boolean containsKey(Object key) {
// Objects.requireNonNull(key);
// return this.map.containsKey(key);
// }

@Override
public boolean isValueContained(V value) {
public boolean containsValue(V value) {
Objects.requireNonNull(value);
return this.map.containsValue(value);
}

// @Override
// @Deprecated
// public boolean containsValue(Object value) {
// Objects.requireNonNull(value);
// return this.map.containsValue(value);
// }

@Override
public Optional<V> getOpt(K key) {
public Optional<V> get(K key) {
Objects.requireNonNull(key);
return Optional.ofNullable(this.map.get(key));
}

// @Override
// @Deprecated
// public V get(Object key) {
// Objects.requireNonNull(key);
// return this.map.get(key);
// }

@Override
public Optional<V> putOpt(K key, V value) {
public Optional<V> put(K key, V value) {
Objects.requireNonNull(key);
Objects.requireNonNull(value);
return Optional.ofNullable(this.map.put(key, value));
}

// @Override
// @Deprecated
// public V put(K key, V value) {
// Objects.requireNonNull(key);
// Objects.requireNonNull(value);
// return this.map.put(key, value);
// }

@Override
public Optional<V> removeOpt(K key) {
public Optional<V> remove(K key) {
Objects.requireNonNull(key);
return Optional.ofNullable(this.map.remove(key));
}

// @Override
// @Deprecated
// public V remove(Object key) {
// Objects.requireNonNull(key);
// return this.map.remove(key);
// }

@Override
public void putAll(Map<? extends K, ? extends V> m) {
Objects.requireNonNull(m);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public List<String> getFields() {
@Override
public Optional<String> getFieldType(String field) {
Objects.requireNonNull(field);
return this.fieldType.getOpt(field);
return this.fieldType.get(field);
}

/**
Expand All @@ -60,7 +60,7 @@ public void declareField(String field, String typeStr) {
throw new ParseException("Field '" + field + "' has been already defined.");
} else {
this.fields.add(field);
this.fieldType.putOpt(field, typeStr);
this.fieldType.put(field, typeStr);
}
}

Expand Down Expand Up @@ -89,7 +89,7 @@ public boolean equals(Object obj) {
@Override
public String toString() {
StringBuffer sbuf = new StringBuffer();
this.fields.forEach(field -> sbuf.append(field + ":" + this.fieldType.getOpt(field).get() + " "));
this.fields.forEach(field -> sbuf.append(field + ":" + this.fieldType.get(field).get() + " "));
return sbuf.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class PrimitiveTypeFactory {
private OptMap<String, PrimitiveType> map = new OptMapImpl<>(new TreeMap<>());

private void add(PrimitiveType primType) {
this.map.putOpt(primType.getTypeName(), primType);
this.map.put(primType.getTypeName(), primType);
}

/**
Expand All @@ -42,7 +42,7 @@ public PrimitiveTypeFactory() {
* primitive type
*/
public boolean contains(String primType) {
return this.map.isKeyContained(primType);
return this.map.containsKey(primType);
}

/**
Expand All @@ -55,7 +55,7 @@ public boolean contains(String primType) {
* @return a new value of the specified type
*/
public PrimitiveTypeValue newInstance(String typeName, String value) {
Optional<PrimitiveType> optPrimType = this.map.getOpt(typeName);
Optional<PrimitiveType> optPrimType = this.map.get(typeName);
if (!optPrimType.isPresent()) {
throw new ParseException("Type '" + typeName + "' is undefined.");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public ExtensionManager(List<Extension> extensions) {
this.extensions.addAll(extensions);
extensions.forEach(extension -> {
String key = extension.getExtensionName();
if (this.extensionMap.isKeyContained(key)) {
if (this.extensionMap.containsKey(key)) {
throw new ExtensionException("Only one implementation is allowed for each extension, and '" + key
+ "' was at least twice.");
}
this.extensionMap.putOpt(key, extension);
this.extensionMap.put(key, extension);
});
}
}
Expand All @@ -56,7 +56,7 @@ public boolean process(List<String> arguments) {
List<String> newArguments = new ArrayList<>();
newArguments.addAll(arguments);
newArguments.remove(0);
Optional<Extension> optExtension = this.extensionMap.getOpt(command);
Optional<Extension> optExtension = this.extensionMap.get(command);
if (!optExtension.isPresent()) {
throw new ExtensionException("Extension '" + command + "' was not found.");
} else if (newArguments.size() < optExtension.get().getRequiredArguments()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public OptMap<URI, URI> parsePrefixMap(String line, int lineCounter) {
} else {
String key = token.substring(0, pos);
String value = token.substring((pos + ParserConstant.PREFIX_SIGN.length()), token.length());
ret.putOpt(asUri(key, lineCounter), asUri(value, lineCounter));
ret.put(asUri(key, lineCounter), asUri(value, lineCounter));
}
}
return ret;
Expand Down Expand Up @@ -184,7 +184,7 @@ public URIValue expandUri(URIValue value, OptMap<URI, URI> prefixMap, int lineCo
int pos = valueStr.indexOf(ParserConstant.PREFIX_SEMICOLON, ParserConstant.PREFIX_AMPERSAND.length());
if (pos != -1) {
URI prefix = asUri(valueStr.substring(ParserConstant.PREFIX_AMPERSAND.length(), pos), lineCounter);
Optional<URI> optExpansion = prefixMap.getOpt(prefix);
Optional<URI> optExpansion = prefixMap.get(prefix);
if (optExpansion.isPresent()) {
ret = new URIValue(
optExpansion.get() + valueStr.substring(pos + ParserConstant.PREFIX_SEMICOLON.length()));
Expand Down Expand Up @@ -332,12 +332,12 @@ public TableMap parseMap(BufferedReader input) throws IOException {
Optional<String> optTableName = getValue(line);
if (optTableName.isPresent()) {
String tableName = optTableName.get();
if (!mapOfTables.isKeyContained(tableName)) {
mapOfTables.putOpt(tableName, new TableImpl());
mapOfRecordIdsOfTables.putOpt(tableName, new TreeSet<>());
if (!mapOfTables.containsKey(tableName)) {
mapOfTables.put(tableName, new TableImpl());
mapOfRecordIdsOfTables.put(tableName, new TreeSet<>());
}
currentTable = mapOfTables.getOpt(tableName).get();
recordIdsOfCurrentTable = mapOfRecordIdsOfTables.getOpt(tableName).get();
currentTable = mapOfTables.get(tableName).get();
recordIdsOfCurrentTable = mapOfRecordIdsOfTables.get(tableName).get();
}

} else if (isTypeDefinition(line)) {
Expand Down Expand Up @@ -375,7 +375,7 @@ record = new RecordImpl();
}

TableMapImpl ret = new TableMapImpl();
mapOfTables.keySet().forEach(key -> ret.put(key, mapOfTables.getOpt(key).get()));
mapOfTables.keySet().forEach(key -> ret.put(key, mapOfTables.get(key).get()));
return ret;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public String renderWithPrefix(String uriStr) {
boolean[] found = new boolean[1];
found[0] = false;
prefixMap.keySet().forEach(key -> {
String expansion = prefixMap.getOpt(key).get().toASCIIString();
String expansion = prefixMap.get(key).get().toASCIIString();
if (!found[0] && uriStr.startsWith(expansion)) {
String keyStr = key.toASCIIString();
if (keyStr.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void renderPrefixMap(UncheckedWriter output, Table table) {
output.write(ParserConstant.SPACE);
output.write(prefix.toASCIIString());
output.write(ParserConstant.TYPE_SIGN);
output.write(table.getPrefixMap().getOpt(prefix).get().toASCIIString());
output.write(table.getPrefixMap().get(prefix).get().toASCIIString());
});
output.write(ParserConstant.NEW_LINE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public Optional<PrimitiveTypeValue> get(String key) {
if (Objects.isNull(key)) {
return Optional.empty();
} else {
return this.map.getOpt(key);
return this.map.get(key);
}
}

@Override
public void set(String key, PrimitiveTypeValue value) {
if (Objects.nonNull(key)) {
this.map.putOpt(key, value);
this.map.put(key, value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public TableImpl(CompositeTypeValue other) {
if (other instanceof Table) {
Table otherTable = (Table) other;
OptMap<URI, URI> otherMap = otherTable.getPrefixMap();
otherMap.keySet().forEach(key -> this.prefixMap.putOpt(key, otherMap.getOpt(key).get()));
otherMap.keySet().forEach(key -> this.prefixMap.put(key, otherMap.get(key).get()));
this.sortingOrder.addAll(otherTable.getSortingOrder());
this.fieldsWithReverseOrder.addAll(otherTable.getFieldsWithReverseOrder());
}
Expand All @@ -65,7 +65,7 @@ public OptMap<URI, URI> getPrefixMap() {
@Override
public void setPrefixMap(OptMap<URI, URI> newPrefixMap) {
this.prefixMap.clear();
newPrefixMap.keySet().forEach(key -> this.prefixMap.putOpt(key, newPrefixMap.getOpt(key).get()));
newPrefixMap.keySet().forEach(key -> this.prefixMap.put(key, newPrefixMap.get(key).get()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public List<String> getTableIds() {
*/
@Override
public void put(String id, Table table) {
this.map.putOpt(id, table);
this.map.put(id, table);
}

/**
Expand All @@ -66,7 +66,7 @@ public void put(String id, Table table) {
*/
@Override
public Optional<Table> getTable(String id) {
return this.map.getOpt(id);
return this.map.get(id);
}

@Override
Expand Down
Loading

0 comments on commit 7359d1e

Please sign in to comment.