001/* 002 * (C) Copyright 2006-2012 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 * Bogdan Stefanescu 018 * Florent Guillaume 019 * Nicolas Chapurlat <nchapurlat@nuxeo.com> 020 */ 021package org.nuxeo.ecm.core.schema.types; 022 023import java.util.HashSet; 024 025import org.nuxeo.ecm.core.schema.types.constraints.Constraint; 026import org.nuxeo.ecm.core.schema.types.resolver.ObjectResolver; 027 028/** 029 * Implementation of a simple type that is not primitive (and therefore has constraints). 030 */ 031public class SimpleTypeImpl extends AbstractType implements SimpleType { 032 033 private static final long serialVersionUID = 1L; 034 035 private ObjectResolver resolver; 036 037 private PrimitiveType primitiveType; 038 039 public SimpleTypeImpl(SimpleType superType, String schema, String name) { 040 super(superType, schema, name); 041 // simple types must have a not null super type 042 // for example a primitive type or another simple type 043 assert superType != null; 044 constraints = new HashSet<Constraint>(); 045 } 046 047 @Override 048 public boolean validate(Object object) throws TypeException { 049 if (object == null) { 050 return true; 051 } 052 if (validateConstraints(object)) { 053 return getSuperType().validate(object); 054 } 055 return false; 056 } 057 058 @Override 059 public PrimitiveType getPrimitiveType() { 060 if (primitiveType == null) { 061 primitiveType = ((SimpleType) getSuperType()).getPrimitiveType(); 062 } 063 return primitiveType; 064 } 065 066 @Override 067 public boolean isPrimitive() { 068 return false; 069 } 070 071 @Override 072 public boolean isSimpleType() { 073 return true; 074 } 075 076 public void setResolver(ObjectResolver resolver) { 077 this.resolver = resolver; 078 } 079 080 @Override 081 public ObjectResolver getObjectResolver() { 082 return resolver; 083 } 084 085 @Override 086 public Object decode(String str) { 087 return getPrimitiveType().decode(str); 088 } 089 090 @Override 091 public String encode(Object value) { 092 return getPrimitiveType().encode(value); 093 } 094 095 @Override 096 public Object convert(Object value) throws TypeException { 097 return getPrimitiveType().convert(value); 098 } 099 100 @Override 101 public Object newInstance() { 102 // XXX AT: not sure that's what is supposed to be done 103 return getPrimitiveType().newInstance(); 104 } 105 106}