From e7ce4ead8e76dc06dfe9dd6f2aa9ea73ad9c3ccc Mon Sep 17 00:00:00 2001 From: ozarov Date: Sun, 4 Jan 2015 21:55:37 -0800 Subject: [PATCH] cleanup --- RobustaSettings.xml | 2 +- pmd.xml | 48 ++++++------------- .../java/com/google/gcloud/RetryHelper.java | 19 ++++---- .../com/google/gcloud/datastore/Cursor.java | 4 +- .../datastore/DatastoreServiceOptions.java | 2 +- .../gcloud/datastore/TransactionOption.java | 9 ++-- .../google/gcloud/datastore/Validator.java | 5 +- .../gcloud/datastore/LocalGcdHelper.java | 3 +- .../gcloud/datastore/SerializationTest.java | 21 ++++---- 9 files changed, 51 insertions(+), 62 deletions(-) diff --git a/RobustaSettings.xml b/RobustaSettings.xml index e957a402fb2a..8c664c57feb7 100644 --- a/RobustaSettings.xml +++ b/RobustaSettings.xml @@ -1,2 +1,2 @@ - + diff --git a/pmd.xml b/pmd.xml index 453c40c8784a..45b3e65a4cc6 100644 --- a/pmd.xml +++ b/pmd.xml @@ -6,22 +6,16 @@ PMD Plugin preferences rule set - - - - - - @@ -31,12 +25,9 @@ - - - @@ -59,7 +50,6 @@ - @@ -67,7 +57,6 @@ - @@ -80,19 +69,24 @@ - + + + + + - - + + + + + - - @@ -107,7 +101,6 @@ - @@ -149,10 +142,8 @@ - - @@ -171,25 +162,20 @@ - - - - - @@ -222,9 +208,7 @@ - - @@ -243,10 +227,13 @@ - + + + + + - @@ -276,9 +263,6 @@ - - - @@ -312,7 +296,6 @@ - @@ -328,7 +311,6 @@ - diff --git a/src/main/java/com/google/gcloud/RetryHelper.java b/src/main/java/com/google/gcloud/RetryHelper.java index ed0fcc1dab85..5e4445361227 100644 --- a/src/main/java/com/google/gcloud/RetryHelper.java +++ b/src/main/java/com/google/gcloud/RetryHelper.java @@ -18,9 +18,9 @@ import java.util.logging.Logger; /** - * Utility class for retrying operations. For more details about the parameters, see - * {@link RetryParams}. If the request is never successful, a {@link RetriesExhaustedException} will - * be thrown. + * Utility class for retrying operations. + * For more details about the parameters, see {@link RetryParams}. + * If the request is never successful, a {@link RetriesExhaustedException} will be thrown. * * @param return value of the closure that is being run with retries */ @@ -99,8 +99,8 @@ public static final class NonRetriableException extends RetryHelperException { private static final long serialVersionUID = -2331878521983499652L; - NonRetriableException(Throwable ex) { - super(ex); + NonRetriableException(Throwable throwable) { + super(throwable); } } @@ -164,12 +164,13 @@ private V doRetry() throws RetryHelperException { log.fine(this + ": attempt #" + attemptNumber + " succeeded"); } return value; + } catch (InterruptedException | InterruptedIOException | ClosedByInterruptException e) { + if (!exceptionHandler.shouldRetry(e)) { + RetryInterruptedException.propagate(); + } + exception = e; } catch (Exception e) { if (!exceptionHandler.shouldRetry(e)) { - if (e instanceof InterruptedException || e instanceof InterruptedIOException - || e instanceof ClosedByInterruptException) { - RetryInterruptedException.propagate(); - } throw new NonRetriableException(e); } exception = e; diff --git a/src/main/java/com/google/gcloud/datastore/Cursor.java b/src/main/java/com/google/gcloud/datastore/Cursor.java index e95f25144c12..bffdfd3dd9ac 100644 --- a/src/main/java/com/google/gcloud/datastore/Cursor.java +++ b/src/main/java/com/google/gcloud/datastore/Cursor.java @@ -65,7 +65,7 @@ public String toUrlSafe() { try { return URLEncoder.encode(toPb().toString(), UTF_8.name()); } catch (UnsupportedEncodingException e) { - throw new RuntimeException("Unxpeced encoding exception", e); + throw new IllegalStateException("Unxpeced encoding exception", e); } } @@ -77,7 +77,7 @@ public static Cursor fromUrlSafe(String urlSafe) { String utf8Str = URLDecoder.decode(urlSafe, UTF_8.name()); return fromPb(DatastoreV1.Value.parseFrom(utf8Str.getBytes())); } catch (UnsupportedEncodingException | InvalidProtocolBufferException e) { - throw new RuntimeException("Unxpeced decoding exception", e); + throw new IllegalStateException("Unxpeced decoding exception", e); } } diff --git a/src/main/java/com/google/gcloud/datastore/DatastoreServiceOptions.java b/src/main/java/com/google/gcloud/datastore/DatastoreServiceOptions.java index f3cc48d69801..009b663ab874 100644 --- a/src/main/java/com/google/gcloud/datastore/DatastoreServiceOptions.java +++ b/src/main/java/com/google/gcloud/datastore/DatastoreServiceOptions.java @@ -35,7 +35,7 @@ public static class Builder extends ServiceOptions.Builder { private String dataset; private String namespace; - private boolean force = false; + private boolean force; private Datastore datastore; private Builder() { diff --git a/src/main/java/com/google/gcloud/datastore/TransactionOption.java b/src/main/java/com/google/gcloud/datastore/TransactionOption.java index 597715df20a8..9de8ea8ff745 100644 --- a/src/main/java/com/google/gcloud/datastore/TransactionOption.java +++ b/src/main/java/com/google/gcloud/datastore/TransactionOption.java @@ -61,6 +61,11 @@ public IsolationLevel(Level level) { public Level level() { return level; } + + @Override + BatchWriteOption toBatchWriteOption() { + return null; + } } TransactionOption() { @@ -89,7 +94,5 @@ static Map, TransactionOption> asImmutableMap return builder.build(); } - BatchWriteOption toBatchWriteOption() { - return null; - } + abstract BatchWriteOption toBatchWriteOption(); } diff --git a/src/main/java/com/google/gcloud/datastore/Validator.java b/src/main/java/com/google/gcloud/datastore/Validator.java index 2f915ccc3d93..235c81b77f16 100644 --- a/src/main/java/com/google/gcloud/datastore/Validator.java +++ b/src/main/java/com/google/gcloud/datastore/Validator.java @@ -9,7 +9,7 @@ /** * Utility to validate Datastore type/values. */ -class Validator { +final class Validator { private static final Pattern DATASET_PATTERN = Pattern.compile( "([a-z\\d\\-]{1,100}~)?([a-z\\d][a-z\\d\\-\\.]{0,99}\\:)?([a-z\\d][a-z\\d\\-]{0,99})"); @@ -17,6 +17,9 @@ class Validator { private static final Pattern NAMESPACE_PATTERN = Pattern.compile(String.format("[0-9A-Za-z\\._\\-]{0,%d}", MAX_NAMESPACE_LENGTH)); + private Validator() { + // utility class + } static String validateDataset(String dataset) { checkArgument(!Strings.isNullOrEmpty(dataset), "dataset can't be empty or null"); diff --git a/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java b/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java index 8be2523de0f8..a4f1ed1ff01f 100644 --- a/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java +++ b/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java @@ -106,7 +106,7 @@ public void start() throws IOException, InterruptedException { File datasetFolder = new File(gcdFolder, GCD + "/" + dataset); datasetFolder.delete(); - // TODO: When System.getProperty("os.name").startsWith("Windows") use cmd.exe /c and gcd.cmd + // TODO: if System.getProperty("os.name").startsWith("Windows") use cmd.exe /c and gcd.cmd Process temp = new ProcessBuilder() .redirectErrorStream(true) .directory(new File(gcdFolder, GCD)) @@ -159,7 +159,6 @@ public void stop() throws IOException, InterruptedException { } if (gcdPath != null) { deleteRecurse(gcdPath); - gcdPath = null; } } diff --git a/src/test/java/com/google/gcloud/datastore/SerializationTest.java b/src/test/java/com/google/gcloud/datastore/SerializationTest.java index 636ab831c8ac..d87b64d7f480 100644 --- a/src/test/java/com/google/gcloud/datastore/SerializationTest.java +++ b/src/test/java/com/google/gcloud/datastore/SerializationTest.java @@ -17,6 +17,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; @@ -97,7 +98,8 @@ public class SerializationTest { private static final ProjectionEntity PROJECTION_ENTITY = ProjectionEntity.fromPb(ENTITY1.toPb()); @SuppressWarnings("rawtypes") - private Multimap typeToValues = ImmutableMultimap.builder() + private static final Multimap TYPE_TO_VALUES = + ImmutableMultimap.builder() .put(Type.NULL, NULL_VALUE) .put(Type.KEY, KEY_VALUE) .put(Type.STRING, STRING_VALUE) @@ -115,7 +117,7 @@ public class SerializationTest { @Test public void testValues() throws Exception { for (Type type : Type.values()) { - for (Value value : typeToValues.get(type)) { + for (Value value : TYPE_TO_VALUES.get(type)) { Value copy = serialiazeAndDeserialize(value); assertEquals(value, value); assertEquals(value, copy); @@ -141,15 +143,14 @@ public void testTypes() throws Exception { } @SuppressWarnings("unchecked") - private T serialiazeAndDeserialize(T obj) throws Exception { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)) { - objectOutputStream.writeObject(obj); + private T serialiazeAndDeserialize(T obj) throws IOException, ClassNotFoundException { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + try (ObjectOutputStream output = new ObjectOutputStream(bytes)) { + output.writeObject(obj); } - byte[] bytes = byteArrayOutputStream.toByteArray(); - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); - try (ObjectInputStream in = new ObjectInputStream(byteArrayInputStream)) { - return (T) in.readObject(); + try (ObjectInputStream input = + new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) { + return (T) input.readObject(); } } }