Address some issues identified by FindBugs.

This commit is contained in:
Greg Burd 2017-10-31 21:10:15 -04:00
parent 01a458a7f6
commit 09a7fbc405
9 changed files with 67 additions and 32 deletions

View file

@ -4,7 +4,6 @@ import com.google.common.base.Optional;
public class DropMaterializedView extends Drop { public class DropMaterializedView extends Drop {
private final String itemType = "MATERIALIZED VIEW";
private Optional<String> keyspaceName = Optional.absent(); private Optional<String> keyspaceName = Optional.absent();
private String itemName; private String itemName;
private boolean ifExists = true; private boolean ifExists = true;
@ -31,7 +30,7 @@ public class DropMaterializedView extends Drop {
@Override @Override
public String buildInternal() { public String buildInternal() {
StringBuilder dropStatement = new StringBuilder("DROP " + itemType + " "); StringBuilder dropStatement = new StringBuilder("DROP MATERIALIZED VIEW ");
if (ifExists) { if (ifExists) {
dropStatement.append("IF EXISTS "); dropStatement.append("IF EXISTS ");
} }

View file

@ -150,8 +150,8 @@ public abstract class AbstractEntityDraft<E> implements Drafted<E> {
Map<String, Object> combined; Map<String, Object> combined;
if (entityMap != null && entityMap.size() > 0) { if (entityMap != null && entityMap.size() > 0) {
combined = new HashMap<String, Object>(entityMap.size()); combined = new HashMap<String, Object>(entityMap.size());
for (String key : entityMap.keySet()) { for (Map.Entry<String, Object> e : entityMap.entrySet()) {
combined.put(key, entityMap.get(key)); combined.put(e.getKey(), e.getValue());
} }
} else { } else {
combined = new HashMap<String, Object>(backingMap.size()); combined = new HashMap<String, Object>(backingMap.size());

View file

@ -124,10 +124,10 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
String database = ""; String database = "";
if (databaseTime.size() > 0) { if (databaseTime.size() > 0) {
List<String> dbt = new ArrayList<>(databaseTime.size()); List<String> dbt = new ArrayList<>(databaseTime.size());
for (String name : databaseTime.keySet()) { for (Map.Entry<String, Double> dt : databaseTime.entrySet()) {
double t = databaseTime.get(name) / 1000.0; double t = dt.getValue() / 1000.0;
d += t; d += t;
dbt.add(String.format("%s took %,.3fms %,2.2f%%", name, t, (t / e) * 100.0)); dbt.add(String.format("%s took %,.3fms %,2.2f%%", dt.getKey(), t, (t / e) * 100.0));
} }
double fd = (d / e) * 100.0; double fd = (d / e) * 100.0;
database = String.format(", %d quer%s (%,.3fms %,2.2f%% - %s)", databaseLookups, database = String.format(", %d quer%s (%,.3fms %,2.2f%% - %s)", databaseLookups,
@ -293,12 +293,13 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
parent.cacheMisses += cacheMisses; parent.cacheMisses += cacheMisses;
parent.databaseLookups += databaseLookups; parent.databaseLookups += databaseLookups;
parent.cacheLookupTime += cacheLookupTime; parent.cacheLookupTime += cacheLookupTime;
for (String name : databaseTime.keySet()) { for (Map.Entry<String, Double> dt : databaseTime.entrySet()) {
String name = dt.getKey();
if (parent.databaseTime.containsKey(name)) { if (parent.databaseTime.containsKey(name)) {
double t = parent.databaseTime.get(name); double t = parent.databaseTime.get(name);
parent.databaseTime.put(name, t + databaseTime.get(name)); parent.databaseTime.put(name, t + dt.getValue());
} else { } else {
parent.databaseTime.put(name, databaseTime.get(name)); parent.databaseTime.put(name, dt.getValue());
} }
} }
} }

View file

@ -335,8 +335,7 @@ public final class HelenusSession extends AbstractSessionOperations implements C
return uow.begin(); return uow.begin();
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException } catch (NoSuchMethodException | InvocationTargetException | InstantiationException
| IllegalAccessException e) { | IllegalAccessException e) {
throw new HelenusException( throw new HelenusException(String.format("Unable to instantiate %s as a UnitOfWork.", unitOfWorkClass.getSimpleName()), e);
String.format("Unable to instantiate {} as a UnitOfWork.", unitOfWorkClass.getSimpleName()), e);
} }
} }
@ -698,6 +697,9 @@ public final class HelenusSession extends AbstractSessionOperations implements C
case UDT : case UDT :
execute(SchemaUtil.dropUserType(entity), true); execute(SchemaUtil.dropUserType(entity), true);
break; break;
default:
throw new HelenusException("Unknown entity type.");
} }
} }
} }

View file

@ -167,10 +167,6 @@ public final class SchemaUtil {
throw new HelenusMappingException("expected view entity " + entity); throw new HelenusMappingException("expected view entity " + entity);
} }
if (entity == null) {
throw new HelenusMappingException("no entity or table to select data");
}
List<HelenusPropertyNode> props = new ArrayList<HelenusPropertyNode>(); List<HelenusPropertyNode> props = new ArrayList<HelenusPropertyNode>();
entity.getOrderedProperties().stream().map(p -> new HelenusPropertyNode(p, Optional.empty())) entity.getOrderedProperties().stream().map(p -> new HelenusPropertyNode(p, Optional.empty()))
.forEach(p -> props.add(p)); .forEach(p -> props.add(p));

View file

@ -8,7 +8,7 @@ public class CacheUtil {
public static List<String[]> combinations(List<String> items) { public static List<String[]> combinations(List<String> items) {
int n = items.size(); int n = items.size();
if (n > 20 || n < 0) if (n > 20)
throw new IllegalArgumentException(n + " is out of range"); throw new IllegalArgumentException(n + " is out of range");
long e = Math.round(Math.pow(2, n)); long e = Math.round(Math.pow(2, n));
List<String[]> out = new ArrayList<String[]>((int) e - 1); List<String[]> out = new ArrayList<String[]>((int) e - 1);

View file

@ -44,13 +44,10 @@ public final class StatementColumnValuePreparer implements ColumnValuePreparer {
HelenusValidator.INSTANCE.validate(prop, value); HelenusValidator.INSTANCE.validate(prop, value);
if (value != null) { Optional<Function<Object, Object>> converter = prop.getWriteConverter(repository);
Optional<Function<Object, Object>> converter = prop.getWriteConverter(repository); if (converter.isPresent()) {
value = converter.get().apply(value);
if (converter.isPresent()) {
value = converter.get().apply(value);
}
} }
return value; return value;

View file

@ -111,13 +111,17 @@ public final class ValueProviderMap implements Map<String, Object> {
@Override @Override
public Set<java.util.Map.Entry<String, Object>> entrySet() { public Set<java.util.Map.Entry<String, Object>> entrySet() {
throwShouldNeverCall("entrySet()"); return entity.getOrderedProperties()
return null; .stream()
.map(p -> {
return new ValueProviderMap.Entry<String, Object>(p.getPropertyName(),
valueProvider.getColumnValue(source, -1, p, immutable));
}).collect(Collectors.toSet());
} }
private void throwShouldNeverCall(String methodName) { private static void throwShouldNeverCall(String methodName) {
throw new HelenusMappingException(String.format( throw new HelenusMappingException(String.format(
"the method {} should never be called on an instance of a Helenus ValueProviderMap", methodName)); "the method %s should never be called on an instance of a Helenus ValueProviderMap", methodName));
} }
@Override @Override
@ -125,6 +129,15 @@ public final class ValueProviderMap implements Map<String, Object> {
return source.toString(); return source.toString();
} }
@Override
public int hashCode() {
int result = source.hashCode();
result = 31 * result + valueProvider.hashCode();
result = 31 * result + entity.hashCode();
result = 31 * result + (immutable ? 1 : 0);
return result;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) if (this == o)
@ -135,10 +148,37 @@ public final class ValueProviderMap implements Map<String, Object> {
Map that = (Map) o; Map that = (Map) o;
if (this.size() != that.size()) if (this.size() != that.size())
return false; return false;
for (String key : this.keySet()) for (Map.Entry<String, Object> e : this.entrySet())
if (!this.get(key).equals(that.get(key))) if (!e.getValue().equals(that.get(e.getKey())))
return false; return false;
return true; return true;
} }
public static class Entry<K, V> implements Map.Entry<K, V> {
private final K key;
private final V value;
public Entry(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V value) {
throwShouldNeverCall("Entry.setValue()");
return null;
}
}
} }

View file

@ -173,9 +173,9 @@ public class UnitOfWorkTest extends AbstractEmbeddedCassandraTest {
.uncached() .uncached()
.sync().orElse(null); .sync().orElse(null);
Assert.assertNotEquals(w5, w2); // Not the same instance Assert.assertNotEquals(w5, w2); // Not the same instance,
Assert.assertTrue(w2.equals(w5)); // But they have the same values Assert.assertTrue(w2.equals(w5)); // but they have the same values,
Assert.assertFalse(w5.equals(w2)); // TODO(gburd): should also work Assert.assertFalse(w5.equals(w2)); // regardless of the order when comparing.
Assert.assertEquals(w5.name(), "Bill"); Assert.assertEquals(w5.name(), "Bill");
uow.commit().andThen(() -> { uow.commit().andThen(() -> {