[hibernate-commits] Hibernate SVN: r19558 - in validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator: cfg and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed May 19 11:52:05 EDT 2010


Author: hardy.ferentschik
Date: 2010-05-19 11:52:05 -0400 (Wed, 19 May 2010)
New Revision: 19558

Added:
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintsForType.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/MinDefinition.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/NotNullDefinition.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/PatternDefinition.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/SizeDefinition.java
Log:
HV-274 First cut of porgrammatic api (work in progress)

Added: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java	2010-05-19 15:52:05 UTC (rev 19558)
@@ -0,0 +1,134 @@
+// $Id:$
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.hibernate.validator.cfg;
+
+import java.lang.annotation.Annotation;
+import java.lang.annotation.ElementType;
+import java.security.AccessController;
+import java.util.HashMap;
+import java.util.Map;
+import javax.validation.ValidationException;
+
+import org.hibernate.validator.util.NewInstance;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class ConstraintDefinition<A extends Annotation> {
+	private final Class<A> constraintType;
+	private final Map<String, Object> parameters;
+	private final Class<?> beanType;
+	private final ElementType elementType;
+	private final String property;
+	private final ConstraintMapping mapping;
+
+	public ConstraintDefinition(Class<?> beanType, Class<A> constraintType, String property, ElementType elementType, ConstraintMapping mapping) {
+		if ( beanType == null ) {
+			throw new ValidationException( "Null is not a valid bean type" );
+		}
+
+		if ( constraintType == null ) {
+			throw new ValidationException( "Null is not a valid constraint type" );
+		}
+
+		if ( mapping == null ) {
+			throw new ValidationException( "ConstraintMapping cannot be null" );
+		}
+
+		if ( ( ElementType.FIELD.equals( elementType ) || ElementType.METHOD.equals( elementType ) )
+				&& ( property == null || property.length() == 0 ) ) {
+			throw new ValidationException( "A property level constraint cannot have a null or empty property name" );
+		}
+
+		this.beanType = beanType;
+		this.constraintType = constraintType;
+		this.parameters = new HashMap<String, Object>();
+		this.property = property;
+		this.elementType = elementType;
+		this.mapping = mapping;
+	}
+
+	protected ConstraintDefinition addParameter(String key, Object value) {
+		parameters.put( key, value );
+		return this;
+	}
+
+	public <A extends Annotation, T extends ConstraintDefinition<A>> T constraint(Class<T> definition) {
+		T constraintDefinition = createConstraintDefinition( definition );
+		mapping.addConstraintConfig( constraintDefinition );
+		return constraintDefinition;
+	}
+
+	public ConstraintsForType property(String property, ElementType type) {
+		return new ConstraintsForType( beanType, property, type, mapping );
+	}
+
+	public ConstraintsForType type(Class<?> type) {
+		return new ConstraintsForType( type, mapping );
+	}
+
+	public ConstraintsForType valid(String property, ElementType type) {
+		return null;
+	}
+
+	public Class<A> getConstraintType() {
+		return constraintType;
+	}
+
+	public Map<String, Object> getParameters() {
+		return parameters;
+	}
+
+	public ElementType getElementType() {
+		return elementType;
+	}
+
+	public Class<?> getBeanType() {
+		return beanType;
+	}
+
+	public String getProperty() {
+		return property;
+	}
+
+	private <A extends Annotation, T extends ConstraintDefinition<A>> T createConstraintDefinition(Class<T> definition) {
+		T constraintDefinition;
+		NewInstance<T> newInstance = NewInstance.action( definition, "", beanType, property, elementType, mapping );
+
+		if ( System.getSecurityManager() != null ) {
+			constraintDefinition = AccessController.doPrivileged( newInstance );
+		}
+		else {
+			constraintDefinition = newInstance.run();
+		}
+		return constraintDefinition;
+	}
+
+	@Override
+	public String toString() {
+		final StringBuilder sb = new StringBuilder();
+		sb.append( "ConstraintDefinition" );
+		sb.append( "{beanType=" ).append( beanType );
+		sb.append( ", constraintType=" ).append( constraintType );
+		sb.append( ", parameters=" ).append( parameters );
+		sb.append( ", elementType=" ).append( elementType );
+		sb.append( ", property='" ).append( property ).append( '\'' );
+		sb.append( '}' );
+		return sb.toString();
+	}
+}

Added: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java	2010-05-19 15:52:05 UTC (rev 19558)
@@ -0,0 +1,65 @@
+// $Id:$
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.hibernate.validator.cfg;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class ConstraintMapping {
+	private final Map<Class<?>, List<ConstraintDefinition<?>>> configData;
+
+	public ConstraintMapping() {
+		configData = new HashMap<Class<?>, List<ConstraintDefinition<?>>>();
+	}
+
+	public ConstraintsForType type(Class<?> beanClass) {
+		return new ConstraintsForType( beanClass, this );
+	}
+
+	protected void addConstraintConfig(ConstraintDefinition<?> definition) {
+		Class<?> beanClass = definition.getBeanType();
+		if(configData.containsKey( beanClass )) {
+			configData.get( beanClass ).add( definition );
+		} else {
+			List<ConstraintDefinition<?>> definitionList = new ArrayList<ConstraintDefinition<?>>();
+			definitionList.add( definition );
+			configData.put(beanClass, definitionList);
+		}
+	}
+
+	public Map<Class<?>, List<ConstraintDefinition<?>>> getConfigData() {
+		return configData;
+	}
+
+	@Override
+	public String toString() {
+		final StringBuilder sb = new StringBuilder();
+		sb.append( "ConstraintMapping" );
+		sb.append( "{configData=" ).append( configData );
+		sb.append( '}' );
+		return sb.toString();
+	}
+}
+
+

Added: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintsForType.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintsForType.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintsForType.java	2010-05-19 15:52:05 UTC (rev 19558)
@@ -0,0 +1,76 @@
+// $Id:$
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.hibernate.validator.cfg;
+
+import java.lang.annotation.Annotation;
+import java.lang.annotation.ElementType;
+import java.security.AccessController;
+
+import org.hibernate.validator.util.NewInstance;
+
+import static java.lang.annotation.ElementType.TYPE;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class ConstraintsForType {
+	private final ConstraintMapping mapping;
+	private final Class<?> beanClass;
+	private String property;
+	private ElementType elementType;
+
+	public ConstraintsForType(Class<?> beanClass, ConstraintMapping mapping) {
+		this( beanClass, "", TYPE, mapping );
+	}
+
+	public ConstraintsForType(Class<?> beanClass, String property, ElementType type, ConstraintMapping mapping) {
+		this.beanClass = beanClass;
+		this.mapping = mapping;
+		this.property = property;
+		this.elementType = type;
+	}
+
+	public <A extends Annotation, T extends ConstraintDefinition<A>> T constraint(Class<T> definition) {
+		T constraintDefinition = createConstraintDefinition( definition );
+		mapping.addConstraintConfig( constraintDefinition );
+		return constraintDefinition;
+	}
+
+	public ConstraintsForType property(String property, ElementType type) {
+		return new ConstraintsForType( beanClass, property, type, mapping );
+	}
+
+	public ConstraintsForType valid(String property, ElementType type) {
+		return null;
+	}
+
+	private <A extends Annotation, T extends ConstraintDefinition<A>> T createConstraintDefinition(Class<T> definition) {
+		T constraintDefinition;
+		NewInstance<T> newInstance = NewInstance.action( definition, "", beanClass, property, elementType, mapping );
+
+		if ( System.getSecurityManager() != null ) {
+			constraintDefinition = AccessController.doPrivileged( newInstance );
+		}
+		else {
+			constraintDefinition = newInstance.run();
+		}
+		return constraintDefinition;
+	}
+}
+
+

Added: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/MinDefinition.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/MinDefinition.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/MinDefinition.java	2010-05-19 15:52:05 UTC (rev 19558)
@@ -0,0 +1,52 @@
+// $Id:$
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.hibernate.validator.cfg;
+
+import java.lang.annotation.ElementType;
+import javax.validation.Payload;
+import javax.validation.constraints.Min;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class MinDefinition extends ConstraintDefinition<Min> {
+
+	public MinDefinition(Class<?> beanType, String property, ElementType elementType, ConstraintMapping mapping) {
+		super( beanType, Min.class, property, elementType, mapping );
+	}
+
+	public MinDefinition message(String message) {
+		addParameter( "message", message );
+		return this;
+	}
+
+	public MinDefinition groups(Class<?>... groups) {
+		addParameter( "groups", groups );
+		return this;
+	}
+
+	public MinDefinition payload(Class<? extends Payload>... payload) {
+		addParameter( "payload", payload );
+		return this;
+	}
+
+	public MinDefinition value(long min) {
+		addParameter( "value", min );
+		return this;
+	}
+}
\ No newline at end of file

Added: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/NotNullDefinition.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/NotNullDefinition.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/NotNullDefinition.java	2010-05-19 15:52:05 UTC (rev 19558)
@@ -0,0 +1,46 @@
+// $Id:$
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.hibernate.validator.cfg;
+
+import java.lang.annotation.ElementType;
+import javax.validation.Payload;
+import javax.validation.constraints.NotNull;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class NotNullDefinition extends ConstraintDefinition<NotNull> {
+	public NotNullDefinition(Class<?> beanType, String property, ElementType elementType, ConstraintMapping mapping) {
+		super( beanType, NotNull.class, property, elementType, mapping );
+	}
+
+	public NotNullDefinition message(String message) {
+		addParameter( "message", message );
+		return this;
+	}
+
+	public NotNullDefinition groups(Class<?>... groups) {
+		addParameter( "groups", groups );
+		return this;
+	}
+
+	public NotNullDefinition payload(Class<? extends Payload>... payload) {
+		addParameter( "payload", payload );
+		return this;
+	}
+}
\ No newline at end of file

Added: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/PatternDefinition.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/PatternDefinition.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/PatternDefinition.java	2010-05-19 15:52:05 UTC (rev 19558)
@@ -0,0 +1,58 @@
+// $Id:$
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.hibernate.validator.cfg;
+
+import java.lang.annotation.ElementType;
+import javax.validation.Payload;
+import javax.validation.constraints.Pattern;
+
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class PatternDefinition extends ConstraintDefinition<Pattern> {
+
+	public PatternDefinition(Class<?> beanType, String property, ElementType elementType, ConstraintMapping mapping) {
+		super( beanType, Pattern.class, property, elementType, mapping );
+	}
+
+	public PatternDefinition message(String message) {
+		addParameter( "message", message );
+		return this;
+	}
+
+	public PatternDefinition groups(Class<?>... groups) {
+		addParameter( "groups", groups );
+		return this;
+	}
+
+	public PatternDefinition payload(Class<? extends Payload>... payload) {
+		addParameter( "payload", payload );
+		return this;
+	}
+
+	public PatternDefinition flags(Pattern.Flag[] flags) {
+		addParameter( "flags", flags );
+		return this;
+	}
+
+	public PatternDefinition regexp(String regexp) {
+		addParameter( "regexp", regexp );
+		return this;
+	}
+}
\ No newline at end of file

Added: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/SizeDefinition.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/SizeDefinition.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/SizeDefinition.java	2010-05-19 15:52:05 UTC (rev 19558)
@@ -0,0 +1,60 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.
+ */
+
+// $Id:$
+package org.hibernate.validator.cfg;
+
+import java.lang.annotation.ElementType;
+import javax.validation.Payload;
+import javax.validation.constraints.Size;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class SizeDefinition extends ConstraintDefinition<Size> {
+
+	public SizeDefinition(Class<?> beanType, String property, ElementType elementType, ConstraintMapping mapping) {
+		super( beanType, Size.class, property, elementType, mapping );
+	}
+
+	public SizeDefinition message(String message) {
+		addParameter( "message", message );
+		return this;
+	}
+
+	public SizeDefinition groups(Class<?>... groups) {
+		addParameter( "groups", groups );
+		return this;
+	}
+
+	public SizeDefinition payload(Class<? extends Payload>... payload) {
+		addParameter( "payload", payload );
+		return this;
+	}
+
+	public SizeDefinition min(int min) {
+		addParameter( "min", min );
+		return this;
+	}
+
+	public SizeDefinition max(int max) {
+		addParameter( "max", max );
+		return this;
+	}
+}
+
+



More information about the hibernate-commits mailing list