Commit 43081eee by Shwetha G S

Merge pull request #105 from shwethags/types

Enforce minimum data requirements for types
parents f7d633de aa27c1bf
......@@ -128,7 +128,6 @@
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
......
/*
* 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.hadoop.metadata;
import java.util.Arrays;
import java.util.Collection;
public class ParamChecker {
/**
* Check that a value is not null. If null throws an IllegalArgumentException.
*
* @param obj value.
* @param name parameter name for the exception message.
* @return the given value.
*/
public static <T> T notNull(T obj, String name) {
if (obj == null) {
throw new IllegalArgumentException(name + " cannot be null");
}
return obj;
}
/**
* Check that a list is not null and that none of its elements is null. If null or if the list has emtpy elements
* throws an IllegalArgumentException.
* @param list the list of T.
* @param name parameter name for the exception message.
*/
public static <T> Collection<T> notNullElements(Collection<T> list, String name) {
notEmpty(list, name);
for (T ele : list) {
notNull(ele, String.format("Collection %s element %s", name, ele));
}
return list;
}
/**
* Check that a list is not null and that none of its elements is null. If null or if the list has emtpy elements
* throws an IllegalArgumentException.
* @param array the array of T.
* @param name parameter name for the exception message.
*/
public static <T> T[] notNullElements(T[] array, String name) {
notEmpty(Arrays.asList(array), name);
for (T ele : array) {
notNull(ele, String.format("Collection %s element %s", name, ele));
}
return array;
}
/**
* Check that a list is not null and not empty.
* @param list the list of T.
* @param name parameter name for the exception message.
*/
public static <T> Collection<T> notEmpty(Collection<T> list, String name) {
notNull(list, name);
if (list.isEmpty()) {
throw new IllegalArgumentException(String.format("Collection %s is empty", name));
}
return list;
}
/**
* Check that a string is not null and not empty. If null or emtpy throws an IllegalArgumentException.
*
* @param value value.
* @param name parameter name for the exception message.
* @return the given value.
*/
public static String notEmpty(String value, String name) {
return notEmpty(value, name, null);
}
/**
* Check that a string is not empty if its not null.
*
* @param value value.
* @param name parameter name for the exception message.
* @return the given value.
*/
public static String notEmptyIfNotNull(String value, String name) {
return notEmptyIfNotNull(value, name, null);
}
/**
* Check that a string is not empty if its not null.
*
* @param value value.
* @param name parameter name for the exception message.
* @return the given value.
*/
public static String notEmptyIfNotNull(String value, String name, String info) {
if (value == null) {
return value;
}
if (value.trim().length() == 0) {
throw new IllegalArgumentException(name + " cannot be empty" + (info == null ? "" : ", " + info));
}
return value.trim();
}
/**
* Check that a string is not null and not empty. If null or emtpy throws an IllegalArgumentException.
*
* @param value value.
* @param name parameter name for the exception message.
* @param info additional information to be printed with the exception message
* @return the given value.
*/
public static String notEmpty(String value, String name, String info) {
if (value == null) {
throw new IllegalArgumentException(name + " cannot be null" + (info == null ? "" : ", " + info));
}
return notEmptyIfNotNull(value, name, info);
}
/**
* Check that a list is not null and that none of its elements is null. If null or if the list has emtpy elements
* throws an IllegalArgumentException.
* @param list the list of strings.
* @param name parameter name for the exception message.
*/
public static Collection<String> notEmptyElements(Collection<String> list, String name) {
notEmpty(list, name);
for (String ele : list) {
notEmpty(ele, String.format("list %s element %s", name, ele));
}
return list;
}
}
......@@ -18,7 +18,7 @@
package org.apache.hadoop.metadata.typesystem.types;
import com.google.common.base.Preconditions;
import org.apache.hadoop.metadata.ParamChecker;
public final class AttributeDefinition {
......@@ -44,16 +44,13 @@ public final class AttributeDefinition {
public AttributeDefinition(String name, String dataTypeName,
Multiplicity multiplicity, boolean isComposite, boolean isUnique,
boolean isIndexable, String reverseAttributeName) {
Preconditions.checkNotNull(name);
Preconditions.checkNotNull(dataTypeName);
this.name = name;
this.dataTypeName = dataTypeName;
this.name = ParamChecker.notEmpty(name, "Attribute name");
this.dataTypeName = ParamChecker.notEmpty(dataTypeName, "Attribute type");
this.multiplicity = multiplicity;
this.isComposite = isComposite;
this.isUnique = isUnique;
this.isIndexable = isIndexable;
this.reverseAttributeName = reverseAttributeName;
this.reverseAttributeName = ParamChecker.notEmptyIfNotNull(reverseAttributeName, "Reverse attribute name");
}
@Override
......
......@@ -18,6 +18,8 @@
package org.apache.hadoop.metadata.typesystem.types;
import org.apache.hadoop.metadata.ParamChecker;
import java.util.Arrays;
public final class EnumTypeDefinition {
......@@ -26,8 +28,8 @@ public final class EnumTypeDefinition {
public final EnumValue[] enumValues;
public EnumTypeDefinition(String name, EnumValue... enumValues) {
this.name = name;
this.enumValues = enumValues;
this.name = ParamChecker.notEmpty(name, "Enum type name");
this.enumValues = ParamChecker.notNullElements(enumValues, "Enum values");
}
@Override
......
......@@ -18,13 +18,15 @@
package org.apache.hadoop.metadata.typesystem.types;
import org.apache.hadoop.metadata.ParamChecker;
public class EnumValue {
public final String value;
public final int ordinal;
public EnumValue(String value, int ordinal) {
this.value = value;
this.value = ParamChecker.notEmpty(value, "Enum value");
this.ordinal = ordinal;
}
......
......@@ -47,7 +47,7 @@ public class HierarchicalTypeDefinition<T extends HierarchicalType> extends Stru
public HierarchicalTypeDefinition(Class<T> hierarchicalMetaType,
String typeName, ImmutableList<String> superTypes,
AttributeDefinition[] attributeDefinitions) {
super(typeName, attributeDefinitions);
super(typeName, false, attributeDefinitions);
hierarchicalMetaTypeName = hierarchicalMetaType.getName();
this.superTypes = superTypes == null ? ImmutableList.<String>of() : superTypes;
}
......
......@@ -18,6 +18,8 @@
package org.apache.hadoop.metadata.typesystem.types;
import org.apache.hadoop.metadata.ParamChecker;
import java.util.Arrays;
public class StructTypeDefinition {
......@@ -25,12 +27,19 @@ public class StructTypeDefinition {
public final String typeName;
public final AttributeDefinition[] attributeDefinitions;
public StructTypeDefinition(String typeName,
AttributeDefinition[] attributeDefinitions) {
this.typeName = typeName;
protected StructTypeDefinition(String typeName, boolean validate, AttributeDefinition... attributeDefinitions) {
this.typeName = ParamChecker.notEmpty(typeName, "Struct type name");
if (attributeDefinitions != null && attributeDefinitions.length != 0) {
ParamChecker.notNullElements(attributeDefinitions, "Attribute definitions");
}
this.attributeDefinitions = attributeDefinitions;
}
public StructTypeDefinition(String typeName, AttributeDefinition[] attributeDefinitions) {
this.typeName = ParamChecker.notEmpty(typeName, "Struct type name");
this.attributeDefinitions = ParamChecker.notNullElements(attributeDefinitions, "Attribute definitions");
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
......
......@@ -291,6 +291,7 @@ public class TypeSystem {
throw new MetadataException(
String.format("Redefinition of type %s not supported", eDef.name));
}
EnumType eT = new EnumType(this, eDef.name, eDef.enumValues);
types.put(eDef.name, eT);
typeCategoriesToTypeNamesMap.put(DataTypes.TypeCategory.ENUM, eDef.name);
......
/*
* 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.hadoop.metadata.typesystem.types;
import com.google.common.collect.ImmutableList;
import org.apache.hadoop.metadata.typesystem.types.utils.TypesUtil;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class ValidationTest {
@DataProvider(name = "attributeData")
private Object[][] createAttributeData() {
return new String[][]{
{null, "type"}, {"", "type"}, {"name", null}, {"name", ""}};
}
@Test (dataProvider = "attributeData", expectedExceptions = {IllegalArgumentException.class})
public void testAttributes(String name, String type) {
TypesUtil.createRequiredAttrDef(name, type);
}
@DataProvider(name = "enumValueData")
private Object[][] createEnumValueData() {
return new String[][]{{null}, {""}};
}
@Test (dataProvider = "enumValueData", expectedExceptions = {IllegalArgumentException.class})
public void testEnumValue(String name) {
new EnumValue(name, 1);
}
@DataProvider(name = "enumTypeData")
private Object[][] createEnumTypeData() {
EnumValue value = new EnumValue("name", 1);
return new Object[][]{{null, value}, {"", value}, {"name"}};
}
@Test (dataProvider = "enumTypeData", expectedExceptions = {IllegalArgumentException.class})
public void testEnumType(String name, EnumValue... values) {
new EnumTypeDefinition(name, values);
}
@DataProvider(name = "structTypeData")
private Object[][] createStructTypeData() {
AttributeDefinition value = TypesUtil.createRequiredAttrDef("name", "type");
return new Object[][]{{null, value}, {"", value}, {"name"}};
}
@Test (dataProvider = "structTypeData", expectedExceptions = {IllegalArgumentException.class})
public void testStructType(String name, AttributeDefinition... values) {
new StructTypeDefinition(name, values);
}
@DataProvider(name = "classTypeData")
private Object[][] createClassTypeData() {
return new Object[][]{{null}, {""}};
}
@Test (dataProvider = "classTypeData", expectedExceptions = {IllegalArgumentException.class})
public void testClassType(String name) {
AttributeDefinition value = TypesUtil.createRequiredAttrDef("name", "type");;
TypesUtil.createClassTypeDef(name, ImmutableList.of("super"), value);
}
@Test (dataProvider = "classTypeData", expectedExceptions = {IllegalArgumentException.class})
public void testTraitType(String name) {
AttributeDefinition value = TypesUtil.createRequiredAttrDef("name", "type");;
TypesUtil.createTraitTypeDef(name, ImmutableList.of("super"), value);
}
@Test
public void testValidTypes() {
AttributeDefinition attribute = TypesUtil.createRequiredAttrDef("name", "type");
//class with no attributes
TypesUtil.createClassTypeDef("name", ImmutableList.of("super"));
//class with no super types
TypesUtil.createClassTypeDef("name", ImmutableList.<String>of(), attribute);
//trait with no attributes
TypesUtil.createTraitTypeDef("name", ImmutableList.of("super"));
//trait with no super types
TypesUtil.createTraitTypeDef("name", ImmutableList.<String>of(), attribute);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment