001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nicolas Chapurlat <nchapurlat@nuxeo.com> 018 */ 019 020package org.nuxeo.ecm.core.schema.types.constraints; 021 022import java.io.Serializable; 023import java.util.HashMap; 024 025import org.nuxeo.ecm.core.schema.types.PrimitiveType; 026import org.nuxeo.ecm.core.schema.types.Type; 027 028/** 029 * This constraint ensures some object's is supported by some {@link Type}. 030 * 031 * @since 7.1 032 */ 033public class TypeConstraint extends AbstractConstraint { 034 035 private static final long serialVersionUID = 1L; 036 037 protected final PrimitiveType type; 038 039 public TypeConstraint(PrimitiveType type) { 040 this.type = type; 041 } 042 043 @Override 044 public boolean validate(Object object) { 045 if (object == null) { 046 return true; 047 } 048 return type.validate(object); 049 } 050 051 /** 052 * <p> 053 * Here, value is : <br> 054 * name = {@value #NAME} <br> 055 * parameters = 056 * <ul> 057 * <li>{@value #PNAME_TYPE} : org.nuxeo.ecm.core.schema.types.primitives.IntegerType</li> 058 * </ul> 059 * </p> 060 */ 061 @Override 062 public Description getDescription() { 063 return new Description(type.getName(), new HashMap<String, Serializable>()); 064 } 065 066 /** 067 * @return The type used by this constraint to validate. 068 * @since 7.1 069 */ 070 public Type getType() { 071 return type; 072 } 073 074 @Override 075 public int hashCode() { 076 final int prime = 31; 077 int result = 1; 078 result = prime * result + ((type == null) ? 0 : type.hashCode()); 079 return result; 080 } 081 082 @Override 083 public boolean equals(Object obj) { 084 if (this == obj) { 085 return true; 086 } 087 if (obj == null) { 088 return false; 089 } 090 if (getClass() != obj.getClass()) { 091 return false; 092 } 093 TypeConstraint other = (TypeConstraint) obj; 094 if (type == null) { 095 if (other.type != null) { 096 return false; 097 } 098 } else if (!type.equals(other.type)) { 099 return false; 100 } 101 return true; 102 } 103 104}