Updated Branches: refs/heads/master ddd23657f -> b94e308e5
http://git-wip-us.apache.org/repos/asf/accumulo/blob/b94e308e/core/src/main/java/org/apache/accumulo/core/data/Condition.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/data/Condition.java b/core/src/main/java/org/apache/accumulo/core/data/Condition.java index 16de324..d5c504d 100644 --- a/core/src/main/java/org/apache/accumulo/core/data/Condition.java +++ b/core/src/main/java/org/apache/accumulo/core/data/Condition.java @@ -16,13 +16,13 @@ */ package org.apache.accumulo.core.data; +import static com.google.common.base.Preconditions.checkArgument; import java.util.Arrays; import java.util.HashSet; import org.apache.accumulo.core.Constants; import org.apache.accumulo.core.client.IteratorSetting; import org.apache.accumulo.core.security.ColumnVisibility; -import org.apache.accumulo.core.util.ArgumentChecker; import org.apache.hadoop.io.Text; /** @@ -51,7 +51,8 @@ public class Condition { * @throws IllegalArgumentException if any argument is null */ public Condition(CharSequence cf, CharSequence cq) { - ArgumentChecker.notNull(cf, cq); + checkArgument(cf != null, "cf is null"); + checkArgument(cq != null, "cq is null"); this.cf = new ArrayByteSequence(cf.toString().getBytes(Constants.UTF8)); this.cq = new ArrayByteSequence(cq.toString().getBytes(Constants.UTF8)); this.cv = EMPTY; @@ -66,7 +67,8 @@ public class Condition { * @throws IllegalArgumentException if any argument is null */ public Condition(byte[] cf, byte[] cq) { - ArgumentChecker.notNull(cf, cq); + checkArgument(cf != null, "cf is null"); + checkArgument(cq != null, "cq is null"); this.cf = new ArrayByteSequence(cf); this.cq = new ArrayByteSequence(cq); this.cv = EMPTY; @@ -81,7 +83,8 @@ public class Condition { * @throws IllegalArgumentException if any argument is null */ public Condition(Text cf, Text cq) { - ArgumentChecker.notNull(cf, cq); + checkArgument(cf != null, "cf is null"); + checkArgument(cq != null, "cq is null"); this.cf = new ArrayByteSequence(cf.getBytes(), 0, cf.getLength()); this.cq = new ArrayByteSequence(cq.getBytes(), 0, cq.getLength()); this.cv = EMPTY; @@ -96,7 +99,8 @@ public class Condition { * @throws IllegalArgumentException if any argument is null */ public Condition(ByteSequence cf, ByteSequence cq) { - ArgumentChecker.notNull(cf, cq); + checkArgument(cf != null, "cf is null"); + checkArgument(cq != null, "cq is null"); this.cf = cf; this.cq = cq; this.cv = EMPTY; @@ -150,7 +154,7 @@ public class Condition { * @throws IllegalArgumentException if value is null */ public Condition setValue(CharSequence value) { - ArgumentChecker.notNull(value); + checkArgument(value != null, "value is null"); this.val = new ArrayByteSequence(value.toString().getBytes(Constants.UTF8)); return this; } @@ -164,7 +168,7 @@ public class Condition { * @throws IllegalArgumentException if value is null */ public Condition setValue(byte[] value) { - ArgumentChecker.notNull(value); + checkArgument(value != null, "value is null"); this.val = new ArrayByteSequence(value); return this; } @@ -179,7 +183,7 @@ public class Condition { * @throws IllegalArgumentException if value is null */ public Condition setValue(Text value) { - ArgumentChecker.notNull(value); + checkArgument(value != null, "value is null"); this.val = new ArrayByteSequence(value.getBytes(), 0, value.getLength()); return this; } @@ -194,7 +198,7 @@ public class Condition { * @throws IllegalArgumentException if value is null */ public Condition setValue(ByteSequence value) { - ArgumentChecker.notNull(value); + checkArgument(value != null, "value is null"); this.val = value; return this; } @@ -215,7 +219,7 @@ public class Condition { * @throws IllegalArgumentException if cv is null */ public Condition setVisibility(ColumnVisibility cv) { - ArgumentChecker.notNull(cv); + checkArgument(cv != null, "cv is null"); this.cv = new ArrayByteSequence(cv.getExpression()); return this; } @@ -241,7 +245,7 @@ public class Condition { * or if any two iterators share the same name or priority */ public Condition setIterators(IteratorSetting... iterators) { - ArgumentChecker.notNull(iterators); + checkArgument(iterators != null, "iterators is null"); if (iterators.length > 1) { HashSet<String> names = new HashSet<String>(); http://git-wip-us.apache.org/repos/asf/accumulo/blob/b94e308e/core/src/main/java/org/apache/accumulo/core/data/ConditionalMutation.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/data/ConditionalMutation.java b/core/src/main/java/org/apache/accumulo/core/data/ConditionalMutation.java index c1206e5..e43968c 100644 --- a/core/src/main/java/org/apache/accumulo/core/data/ConditionalMutation.java +++ b/core/src/main/java/org/apache/accumulo/core/data/ConditionalMutation.java @@ -17,12 +17,12 @@ package org.apache.accumulo.core.data; +import static com.google.common.base.Preconditions.checkArgument; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -import org.apache.accumulo.core.util.ArgumentChecker; import org.apache.hadoop.io.Text; /** @@ -66,12 +66,12 @@ public class ConditionalMutation extends Mutation { } private void init(Condition... conditions) { - ArgumentChecker.notNull(conditions); + checkArgument(conditions != null, "conditions is null"); this.conditions.addAll(Arrays.asList(conditions)); } public void addCondition(Condition condition) { - ArgumentChecker.notNull(condition); + checkArgument(condition != null, "condition is null"); this.conditions.add(condition); } http://git-wip-us.apache.org/repos/asf/accumulo/blob/b94e308e/core/src/main/java/org/apache/accumulo/core/metadata/MetadataServicer.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/metadata/MetadataServicer.java b/core/src/main/java/org/apache/accumulo/core/metadata/MetadataServicer.java index 67f1d8a..7a503b7 100644 --- a/core/src/main/java/org/apache/accumulo/core/metadata/MetadataServicer.java +++ b/core/src/main/java/org/apache/accumulo/core/metadata/MetadataServicer.java @@ -16,6 +16,7 @@ */ package org.apache.accumulo.core.metadata; +import static com.google.common.base.Preconditions.checkArgument; import java.util.SortedMap; import org.apache.accumulo.core.client.AccumuloException; @@ -25,7 +26,6 @@ import org.apache.accumulo.core.client.Instance; import org.apache.accumulo.core.client.TableNotFoundException; import org.apache.accumulo.core.data.KeyExtent; import org.apache.accumulo.core.security.Credentials; -import org.apache.accumulo.core.util.ArgumentChecker; /** * Provides a consolidated API for handling table metadata @@ -33,13 +33,13 @@ import org.apache.accumulo.core.util.ArgumentChecker; public abstract class MetadataServicer { public static MetadataServicer forTableName(Instance instance, Credentials credentials, String tableName) throws AccumuloException, AccumuloSecurityException { - ArgumentChecker.notNull(tableName); + checkArgument(tableName != null, "tableName is null"); Connector conn = instance.getConnector(credentials.getPrincipal(), credentials.getToken()); return forTableId(instance, credentials, conn.tableOperations().tableIdMap().get(tableName)); } public static MetadataServicer forTableId(Instance instance, Credentials credentials, String tableId) { - ArgumentChecker.notNull(tableId); + checkArgument(tableId != null, "tableId is null"); if (RootTable.ID.equals(tableId)) return new ServicerForRootTable(instance, credentials); else if (MetadataTable.ID.equals(tableId)) http://git-wip-us.apache.org/repos/asf/accumulo/blob/b94e308e/core/src/main/java/org/apache/accumulo/core/security/Authorizations.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/security/Authorizations.java b/core/src/main/java/org/apache/accumulo/core/security/Authorizations.java index ab3ea68..1571c62 100644 --- a/core/src/main/java/org/apache/accumulo/core/security/Authorizations.java +++ b/core/src/main/java/org/apache/accumulo/core/security/Authorizations.java @@ -16,6 +16,7 @@ */ package org.apache.accumulo.core.security; +import static com.google.common.base.Preconditions.checkArgument; import java.io.Serializable; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -30,7 +31,6 @@ import java.util.TreeSet; import org.apache.accumulo.core.Constants; import org.apache.accumulo.core.data.ArrayByteSequence; import org.apache.accumulo.core.data.ByteSequence; -import org.apache.accumulo.core.util.ArgumentChecker; import org.apache.accumulo.core.util.ByteBufferUtil; import org.apache.commons.codec.binary.Base64; @@ -109,7 +109,7 @@ public class Authorizations implements Iterable<byte[]>, Serializable, Authoriza * @see #Authorizations(String...) */ public Authorizations(Collection<byte[]> authorizations) { - ArgumentChecker.notNull(authorizations); + checkArgument(authorizations != null, "authorizations is null"); for (byte[] auth : authorizations) auths.add(new ArrayByteSequence(auth)); checkAuths(); @@ -126,7 +126,7 @@ public class Authorizations implements Iterable<byte[]>, Serializable, Authoriza * @see #Authorizations(String...) */ public Authorizations(List<ByteBuffer> authorizations) { - ArgumentChecker.notNull(authorizations); + checkArgument(authorizations != null, "authorizations is null"); for (ByteBuffer buffer : authorizations) { auths.add(new ArrayByteSequence(ByteBufferUtil.toBytes(buffer))); } @@ -144,7 +144,7 @@ public class Authorizations implements Iterable<byte[]>, Serializable, Authoriza */ public Authorizations(byte[] authorizations) { - ArgumentChecker.notNull(authorizations); + checkArgument(authorizations != null, "authorizations is null"); String authsString = new String(authorizations, Constants.UTF8); if (authsString.startsWith(HEADER)) { @@ -159,7 +159,7 @@ public class Authorizations implements Iterable<byte[]>, Serializable, Authoriza } } else { // it's the old format - ArgumentChecker.notNull(authorizations); + checkArgument(authorizations != null, "authorizations is null"); if (authorizations.length > 0) setAuthorizations(authsString.split(",")); } @@ -185,7 +185,7 @@ public class Authorizations implements Iterable<byte[]>, Serializable, Authoriza } private void setAuthorizations(String... authorizations) { - ArgumentChecker.notNull(authorizations); + checkArgument(authorizations != null, "authorizations is null"); auths.clear(); for (String str : authorizations) { str = str.trim(); http://git-wip-us.apache.org/repos/asf/accumulo/blob/b94e308e/core/src/main/java/org/apache/accumulo/core/util/ArgumentChecker.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/util/ArgumentChecker.java b/core/src/main/java/org/apache/accumulo/core/util/ArgumentChecker.java deleted file mode 100644 index 1ba133a..0000000 --- a/core/src/main/java/org/apache/accumulo/core/util/ArgumentChecker.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.accumulo.core.util; - -/** - * This class provides methods to check arguments of a variable number for null values, or anything else that might be required on a routine basis. These - * methods should be used for early failures as close to the end user as possible, so things do not fail later on the server side, when they are harder to - * debug. - * - * Methods are created for a specific number of arguments, due to the poor performance of array allocation for varargs methods. - */ -public class ArgumentChecker { - private static final String NULL_ARG_MSG = "argument was null"; - - public static final void notNull(final Object arg1) { - if (arg1 == null) - throw new IllegalArgumentException(NULL_ARG_MSG + ":Is null- arg1? " + (arg1 == null)); - } - - public static final void notNull(final Object arg1, final Object arg2) { - if (arg1 == null || arg2 == null) - throw new IllegalArgumentException(NULL_ARG_MSG + ":Is null- arg1? " + (arg1 == null) + " arg2? " + (arg2 == null)); - } - - public static final void notNull(final Object arg1, final Object arg2, final Object arg3) { - if (arg1 == null || arg2 == null || arg3 == null) - throw new IllegalArgumentException(NULL_ARG_MSG + ":Is null- arg1? " + (arg1 == null) + " arg2? " + (arg2 == null) + " arg3? " + (arg3 == null)); - } - - public static final void notNull(final Object arg1, final Object arg2, final Object arg3, final Object arg4) { - if (arg1 == null || arg2 == null || arg3 == null || arg4 == null) - throw new IllegalArgumentException(NULL_ARG_MSG + ":Is null- arg1? " + (arg1 == null) + " arg2? " + (arg2 == null) + " arg3? " + (arg3 == null) - + " arg4? " + (arg4 == null)); - } - - public static final void notNull(final Object[] args) { - if (args == null) - throw new IllegalArgumentException(NULL_ARG_MSG + ":arg array is null"); - - for (int i = 0; i < args.length; i++) - if (args[i] == null) - throw new IllegalArgumentException(NULL_ARG_MSG + ":arg" + i + " is null"); - } - - public static final void strictlyPositive(final int i) { - if (i <= 0) - throw new IllegalArgumentException("integer should be > 0, was " + i); - } - - public static final void notEmpty(Iterable<?> arg) { - if (!arg.iterator().hasNext()) - throw new IllegalArgumentException("Argument should not be empty"); - } - - public static abstract class Validator<T> { - - public final T validate(final T argument) throws IllegalArgumentException { - if (!isValid(argument)) - throw new IllegalArgumentException(invalidMessage(argument)); - return argument; - } - - public abstract boolean isValid(final T argument); - - public abstract String invalidMessage(final T argument); - - public Validator<T> and(final Validator<T> other) { - if (other == null) - return this; - final Validator<T> mine = this; - return new Validator<T>() { - - @Override - public boolean isValid(T argument) { - return mine.isValid(argument) && other.isValid(argument); - } - - @Override - public String invalidMessage(T argument) { - return (mine.isValid(argument) ? other : mine).invalidMessage(argument); - } - - }; - } - } - -} http://git-wip-us.apache.org/repos/asf/accumulo/blob/b94e308e/core/src/main/java/org/apache/accumulo/core/util/Validator.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/util/Validator.java b/core/src/main/java/org/apache/accumulo/core/util/Validator.java new file mode 100644 index 0000000..efb70c0 --- /dev/null +++ b/core/src/main/java/org/apache/accumulo/core/util/Validator.java @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.accumulo.core.util; + +/** + * A class that validates arguments of a particular type. Implementations must + * implement {@link #isValid(Object)} and should override {@link #invalidMessage(Object)}. + */ +public abstract class Validator<T> { + + /** + * Validates an argument. + * + * @param argument + * argument to validate + * @return the argument, if validation passes + * @throws IllegalArgumentException + * if validation fails + */ + public final T validate(final T argument) { + if (!isValid(argument)) + throw new IllegalArgumentException(invalidMessage(argument)); + return argument; + } + + /** + * Checks an argument for validity. + * + * @param argument + * argument to validate + * @return true if valid, false if invalid + */ + public abstract boolean isValid(final T argument); + + /** + * Formulates an exception message for invalid values. + * + * @param argument + * argument that failed validation + * @return exception message + */ + public String invalidMessage(final T argument) { + return String.format("Invalid argument %s", argument); + } + + /** + * Creates a new validator that is the conjunction of this one and the given one. An argument passed to the returned validator is valid only if it passes both + * validators. + * + * @param other + * other validator + * @return combined validator + */ + public final Validator<T> and(final Validator<T> other) { + if (other == null) + return this; + final Validator<T> mine = this; + return new Validator<T>() { + + @Override + public boolean isValid(T argument) { + return mine.isValid(argument) && other.isValid(argument); + } + + @Override + public String invalidMessage(T argument) { + return (mine.isValid(argument) ? other : mine).invalidMessage(argument); + } + + }; + } + + /** + * Creates a new validator that is the disjunction of this one and the given one. An argument passed to the returned validator is valid only if it passes at + * least one of the validators. + * + * @param other + * other validator + * @return combined validator + */ + public final Validator<T> or(final Validator<T> other) { + if (other == null) + return this; + final Validator<T> mine = this; + return new Validator<T>() { + + @Override + public boolean isValid(T argument) { + return mine.isValid(argument) || other.isValid(argument); + } + + @Override + public String invalidMessage(T argument) { + return mine.invalidMessage(argument); + } + + }; + } + + /** + * Creates a new validator that is the negation of this one. An argument passed to the returned validator is valid only if it fails this one. + * + * @return negated validator + */ + public final Validator<T> not() { + final Validator<T> mine = this; + return new Validator<T>() { + + @Override + public boolean isValid(T argument) { + return !mine.isValid(argument); + } + + @Override + public String invalidMessage(T argument) { + return "Validation should have failed with: " + mine.invalidMessage(argument); + } + + }; + } +} http://git-wip-us.apache.org/repos/asf/accumulo/blob/b94e308e/core/src/test/java/org/apache/accumulo/core/util/ValidatorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/util/ValidatorTest.java b/core/src/test/java/org/apache/accumulo/core/util/ValidatorTest.java new file mode 100644 index 0000000..6fdeabc --- /dev/null +++ b/core/src/test/java/org/apache/accumulo/core/util/ValidatorTest.java @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.accumulo.core.util; + +import java.util.regex.Pattern; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +public class ValidatorTest { + private static class TestValidator extends Validator<String> { + private final String s; + + TestValidator(String s) { + this.s = s; + } + + @Override + public boolean isValid(String argument) { + return s.equals(argument); + } + } + + private static class Test2Validator extends Validator<String> { + private final String ps; + + Test2Validator(String s) { + ps = s; + } + + @Override + public boolean isValid(String argument) { + return (argument != null && argument.matches(ps)); + } + } + + private Validator<String> v, v2, v3; + + @Before + public void setUp() { + v = new TestValidator("correct"); + v2 = new TestValidator("righto"); + v3 = new Test2Validator("c.*"); + } + + @Test + public void testValidate_Success() { + assertEquals("correct", v.validate("correct")); + } + + @Test(expected = IllegalArgumentException.class) + public void testValidate_Failure() { + v.validate("incorrect"); + } + + @Test + public void testInvalidMessage() { + assertEquals("Invalid argument incorrect", v.invalidMessage("incorrect")); + } + + @Test + public void testAnd() { + Validator<String> vand = v3.and(v); + assertTrue(vand.isValid("correct")); + assertFalse(vand.isValid("righto")); + assertFalse(vand.isValid("coriander")); + } + + @Test + public void testOr() { + Validator<String> vor = v.or(v2); + assertTrue(vor.isValid("correct")); + assertTrue(vor.isValid("righto")); + assertFalse(vor.isValid("coriander")); + } + + @Test + public void testNot() { + Validator<String> vnot = v3.not(); + assertFalse(vnot.isValid("correct")); + assertFalse(vnot.isValid("coriander")); + assertTrue(vnot.isValid("righto")); + } +} http://git-wip-us.apache.org/repos/asf/accumulo/blob/b94e308e/server/base/src/main/java/org/apache/accumulo/server/trace/TraceFileSystem.java ---------------------------------------------------------------------- diff --git a/server/base/src/main/java/org/apache/accumulo/server/trace/TraceFileSystem.java b/server/base/src/main/java/org/apache/accumulo/server/trace/TraceFileSystem.java index 71cc562..d3fbad7 100644 --- a/server/base/src/main/java/org/apache/accumulo/server/trace/TraceFileSystem.java +++ b/server/base/src/main/java/org/apache/accumulo/server/trace/TraceFileSystem.java @@ -16,10 +16,10 @@ */ package org.apache.accumulo.server.trace; +import static com.google.common.base.Preconditions.checkArgument; import java.io.IOException; import java.net.URI; -import org.apache.accumulo.core.util.ArgumentChecker; import org.apache.accumulo.trace.instrument.Span; import org.apache.accumulo.trace.instrument.Trace; import org.apache.hadoop.conf.Configuration; @@ -667,7 +667,7 @@ public class TraceFileSystem extends FileSystem { final FileSystem impl; TraceFileSystem(FileSystem impl) { - ArgumentChecker.notNull(impl); + checkArgument(impl != null, "impl is null"); this.impl = impl; } http://git-wip-us.apache.org/repos/asf/accumulo/blob/b94e308e/server/master/src/main/java/org/apache/accumulo/master/FateServiceHandler.java ---------------------------------------------------------------------- diff --git a/server/master/src/main/java/org/apache/accumulo/master/FateServiceHandler.java b/server/master/src/main/java/org/apache/accumulo/master/FateServiceHandler.java index 7e274c0..671381f 100644 --- a/server/master/src/main/java/org/apache/accumulo/master/FateServiceHandler.java +++ b/server/master/src/main/java/org/apache/accumulo/master/FateServiceHandler.java @@ -39,8 +39,8 @@ import org.apache.accumulo.core.iterators.IteratorUtil; import org.apache.accumulo.core.master.thrift.FateOperation; import org.apache.accumulo.core.master.thrift.FateService; import org.apache.accumulo.core.security.thrift.TCredentials; -import org.apache.accumulo.core.util.ArgumentChecker.Validator; import org.apache.accumulo.core.util.ByteBufferUtil; +import org.apache.accumulo.core.util.Validator; import org.apache.accumulo.fate.TStore.TStatus; import org.apache.accumulo.master.tableOps.BulkImport; import org.apache.accumulo.master.tableOps.CancelCompactions;