avoid npe chance, use == with enums

This commit is contained in:
Dave Brosius 2014-11-15 16:22:33 -05:00
parent 9c564f10fd
commit 2c3f9ba048
8 changed files with 40 additions and 38 deletions

View File

@ -152,17 +152,17 @@ public class DataResource implements IResource
public boolean isRootLevel()
{
return level.equals(Level.ROOT);
return level == Level.ROOT;
}
public boolean isKeyspaceLevel()
{
return level.equals(Level.KEYSPACE);
return level == Level.KEYSPACE;
}
public boolean isColumnFamilyLevel()
{
return level.equals(Level.COLUMN_FAMILY);
return level == Level.COLUMN_FAMILY;
}
/**
* @return keyspace of the resource. Throws IllegalStateException if it's the root-level resource.
@ -187,14 +187,16 @@ public class DataResource implements IResource
/**
* @return Whether or not the resource has a parent in the hierarchy.
*/
@Override
public boolean hasParent()
{
return !level.equals(Level.ROOT);
return level != Level.ROOT;
}
/**
* @return Whether or not the resource exists in Cassandra.
*/
@Override
public boolean exists()
{
switch (level)

View File

@ -188,7 +188,7 @@ public class CachingOptions
public boolean isEnabled()
{
return type.equals(Type.ALL);
return type == Type.ALL;
}
@Override
@ -223,7 +223,7 @@ public class CachingOptions
public RowCache(Type type)
{
this(type, type.equals(Type.ALL) ? Integer.MAX_VALUE : 0);
this(type, (type == Type.ALL) ? Integer.MAX_VALUE : 0);
}
public RowCache(Type type, int rowsToCache)
{
@ -246,17 +246,17 @@ public class CachingOptions
}
public boolean isEnabled()
{
return type.equals(Type.ALL) || type.equals(Type.HEAD);
return (type == Type.ALL) || (type == Type.HEAD);
}
public boolean cacheFullPartitions()
{
return type.equals(Type.ALL);
return type == Type.ALL;
}
@Override
public String toString()
{
if (type.equals(Type.ALL)) return "ALL";
if (type.equals(Type.NONE)) return "NONE";
if (type == Type.ALL) return "ALL";
if (type == Type.NONE) return "NONE";
return String.valueOf(rowsToCache);
}

View File

@ -815,7 +815,7 @@ public final class CFMetaData
throw new ConfigurationException(String.format("Column family ID mismatch (found %s; expected %s)",
cfm.cfId, cfId));
if (!cfm.cfType.equals(cfType))
if (cfm.cfType != cfType)
throw new ConfigurationException("types do not match.");
if (!cfm.comparator.isCompatibleWith(comparator))

View File

@ -59,7 +59,7 @@ public class ColumnCondition
this.inValues = inValues;
this.operator = op;
if (!operator.equals(Operator.IN))
if (operator != Operator.IN)
assert this.inValues == null;
}
@ -117,7 +117,7 @@ public class ColumnCondition
if (collectionElement != null)
collectionElement.collectMarkerSpecification(boundNames);
if (operator.equals(Operator.IN) && inValues != null)
if ((operator == Operator.IN) && inValues != null)
{
for (Term value : inValues)
value.collectMarkerSpecification(boundNames);
@ -130,7 +130,7 @@ public class ColumnCondition
public ColumnCondition.Bound bind(QueryOptions options) throws InvalidRequestException
{
boolean isInCondition = operator.equals(Operator.IN);
boolean isInCondition = operator == Operator.IN;
if (column.type instanceof CollectionType)
{
if (collectionElement == null)
@ -186,7 +186,7 @@ public class ColumnCondition
else if (otherValue == null)
{
// the condition value is not null, so only NEQ can return true
return operator.equals(Operator.NEQ);
return operator == Operator.NEQ;
}
int comparison = type.compare(otherValue, value);
switch (operator)
@ -236,7 +236,7 @@ public class ColumnCondition
{
super(condition.column, condition.operator);
assert !(column.type instanceof CollectionType) && condition.collectionElement == null;
assert !condition.operator.equals(Operator.IN);
assert condition.operator != Operator.IN;
this.value = condition.value.bindAndGet(options);
}
@ -258,7 +258,7 @@ public class ColumnCondition
{
super(condition.column, condition.operator);
assert !(column.type instanceof CollectionType) && condition.collectionElement == null;
assert condition.operator.equals(Operator.IN);
assert condition.operator == Operator.IN;
if (condition.inValues == null)
this.inValues = ((Lists.Marker) condition.value).bind(options).getElements();
else
@ -291,7 +291,7 @@ public class ColumnCondition
{
super(condition.column, condition.operator);
assert column.type instanceof CollectionType && condition.collectionElement != null;
assert !condition.operator.equals(Operator.IN);
assert condition.operator != Operator.IN;
this.collectionElement = condition.collectionElement.bindAndGet(options);
this.value = condition.value.bindAndGet(options);
}
@ -468,7 +468,7 @@ public class ColumnCondition
{
super(condition.column, condition.operator);
assert column.type.isCollection() && condition.collectionElement == null;
assert !condition.operator.equals(Operator.IN);
assert condition.operator != Operator.IN;
this.value = condition.value.bind(options);
}
@ -481,9 +481,9 @@ public class ColumnCondition
Iterator<Cell> iter = collectionColumns(current.metadata().comparator.create(rowPrefix, column), current, now);
if (value == null)
{
if (operator.equals(Operator.EQ))
if (operator == Operator.EQ)
return !iter.hasNext();
else if (operator.equals(Operator.NEQ))
else if (operator == Operator.NEQ)
return iter.hasNext();
else
throw new InvalidRequestException(String.format("Invalid comparison with null for operator \"%s\"", operator));
@ -535,7 +535,7 @@ public class ColumnCondition
while(iter.hasNext())
{
if (!conditionIter.hasNext())
return operator.equals(Operator.GT) || operator.equals(Operator.GTE) || operator.equals(Operator.NEQ);
return (operator == Operator.GT) || (operator == Operator.GTE) || (operator == Operator.NEQ);
// for lists we use the cell value; for sets we use the cell name
ByteBuffer cellValue = isSet? iter.next().name().collectionElement() : iter.next().value();
@ -545,7 +545,7 @@ public class ColumnCondition
}
if (conditionIter.hasNext())
return operator.equals(Operator.LT) || operator.equals(Operator.LTE) || operator.equals(Operator.NEQ);
return (operator == Operator.LT) || (operator == Operator.LTE) || (operator == Operator.NEQ);
// they're equal
return operator == Operator.EQ || operator == Operator.LTE || operator == Operator.GTE;
@ -590,7 +590,7 @@ public class ColumnCondition
while(iter.hasNext())
{
if (!conditionIter.hasNext())
return operator.equals(Operator.GT) || operator.equals(Operator.GTE) || operator.equals(Operator.NEQ);
return (operator == Operator.GT) || (operator == Operator.GTE) || (operator == Operator.NEQ);
Map.Entry<ByteBuffer, ByteBuffer> conditionEntry = conditionIter.next();
Cell c = iter.next();
@ -607,7 +607,7 @@ public class ColumnCondition
}
if (conditionIter.hasNext())
return operator.equals(Operator.LT) || operator.equals(Operator.LTE) || operator.equals(Operator.NEQ);
return (operator == Operator.LT) || (operator == Operator.LTE) || (operator == Operator.NEQ);
// they're equal
return operator == Operator.EQ || operator == Operator.LTE || operator == Operator.GTE;
@ -622,7 +622,7 @@ public class ColumnCondition
{
super(condition.column, condition.operator);
assert column.type instanceof CollectionType && condition.collectionElement == null;
assert condition.operator.equals(Operator.IN);
assert condition.operator == Operator.IN;
inValues = new ArrayList<>();
if (condition.inValues == null)
{
@ -768,7 +768,7 @@ public class ColumnCondition
if (collectionElement == null)
{
if (operator.equals(Operator.IN))
if (operator == Operator.IN)
{
if (inValues == null)
return ColumnCondition.inCondition(receiver, inMarker.prepare(keyspace, receiver));
@ -802,7 +802,7 @@ public class ColumnCondition
default:
throw new AssertionError();
}
if (operator.equals(Operator.IN))
if (operator == Operator.IN)
{
if (inValues == null)
return ColumnCondition.inCondition(receiver, collectionElement.prepare(keyspace, elementSpec), inMarker.prepare(keyspace, valueSpec));

View File

@ -1422,7 +1422,7 @@ public class SelectStatement implements CQLStatement, MeasurableForPreparedCache
hasQueriableIndex |= queriable[0];
hasQueriableClusteringColumnIndex |= queriable[1];
names.add(def);
hasMultiColumnRelations |= ColumnDefinition.Kind.CLUSTERING_COLUMN.equals(def.kind);
hasMultiColumnRelations |= ColumnDefinition.Kind.CLUSTERING_COLUMN == def.kind;
}
updateRestrictionsForRelation(stmt, names, rel, boundNames);
}
@ -1434,7 +1434,7 @@ public class SelectStatement implements CQLStatement, MeasurableForPreparedCache
boolean[] queriable = processRelationEntity(stmt, indexManager, relation, entity, def);
hasQueriableIndex |= queriable[0];
hasQueriableClusteringColumnIndex |= queriable[1];
hasSingleColumnRelations |= ColumnDefinition.Kind.CLUSTERING_COLUMN.equals(def.kind);
hasSingleColumnRelations |= ColumnDefinition.Kind.CLUSTERING_COLUMN == def.kind;
updateRestrictionsForRelation(stmt, def, rel, boundNames);
}
}

View File

@ -312,7 +312,7 @@ public class DataTracker
public void replaceEarlyOpenedFiles(Collection<SSTableReader> toReplace, Collection<SSTableReader> replaceWith)
{
for (SSTableReader s : toReplace)
assert s.openReason.equals(SSTableReader.OpenReason.EARLY);
assert s.openReason == SSTableReader.OpenReason.EARLY;
// note that we can replace an early opened file with a real one
replaceReaders(toReplace, replaceWith, false);
}

View File

@ -235,10 +235,10 @@ public class ClientState
return;
validateLogin();
preventSystemKSSchemaModification(keyspace, resource, perm);
if (perm.equals(Permission.SELECT) && READABLE_SYSTEM_RESOURCES.contains(resource))
if ((perm == Permission.SELECT) && READABLE_SYSTEM_RESOURCES.contains(resource))
return;
if (PROTECTED_AUTH_RESOURCES.contains(resource))
if (perm.equals(Permission.CREATE) || perm.equals(Permission.ALTER) || perm.equals(Permission.DROP))
if ((perm == Permission.CREATE) || (perm == Permission.ALTER) || (perm == Permission.DROP))
throw new UnauthorizedException(String.format("%s schema is protected", resource));
ensureHasPermission(perm, resource);
}
@ -258,7 +258,7 @@ public class ClientState
private void preventSystemKSSchemaModification(String keyspace, DataResource resource, Permission perm) throws UnauthorizedException
{
// we only care about schema modification.
if (!(perm.equals(Permission.ALTER) || perm.equals(Permission.DROP) || perm.equals(Permission.CREATE)))
if (!((perm == Permission.ALTER) || (perm == Permission.DROP) || (perm == Permission.CREATE)))
return;
// prevent system keyspace modification
@ -267,7 +267,7 @@ public class ClientState
// we want to allow altering AUTH_KS and TRACING_KS.
Set<String> allowAlter = Sets.newHashSet(Auth.AUTH_KS, TraceKeyspace.NAME);
if (allowAlter.contains(keyspace.toLowerCase()) && !(resource.isKeyspaceLevel() && perm.equals(Permission.ALTER)))
if (allowAlter.contains(keyspace.toLowerCase()) && !(resource.isKeyspaceLevel() && (perm == Permission.ALTER)))
throw new UnauthorizedException(String.format("Cannot %s %s", perm, resource));
}

View File

@ -1335,7 +1335,7 @@ public class StorageService extends NotificationBroadcasterSupport implements IE
*/
public void onChange(InetAddress endpoint, ApplicationState state, VersionedValue value)
{
if (state.equals(ApplicationState.STATUS))
if (state == ApplicationState.STATUS)
{
String apStateValue = value.value;
String[] pieces = apStateValue.split(VersionedValue.DELIMITER_STR, -1);
@ -2194,7 +2194,7 @@ public class StorageService extends NotificationBroadcasterSupport implements IE
*/
public void takeSnapshot(String tag, String... keyspaceNames) throws IOException
{
if (operationMode.equals(Mode.JOINING))
if (operationMode == Mode.JOINING)
throw new IOException("Cannot snapshot until bootstrap completes");
if (tag == null || tag.equals(""))
throw new IOException("You must supply a snapshot name.");
@ -2233,7 +2233,7 @@ public class StorageService extends NotificationBroadcasterSupport implements IE
{
if (keyspaceName == null)
throw new IOException("You must supply a keyspace name");
if (operationMode.equals(Mode.JOINING))
if (operationMode == Mode.JOINING)
throw new IOException("Cannot snapshot until bootstrap completes");
if (columnFamilyName == null)