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.math.BigDecimal; 023import java.text.ParseException; 024import java.text.SimpleDateFormat; 025import java.util.Calendar; 026import java.util.Collection; 027import java.util.Date; 028 029/** 030 * Some usefull utils for Constraint API. 031 * 032 * @since 7.1 033 */ 034public final class ConstraintUtils { 035 036 public static final String DATE_FORMAT = "yyyy-MM-dd"; 037 038 private ConstraintUtils() { 039 } 040 041 /** 042 * @return a date formatter xsd compliant : {@value #DATE_FORMAT} 043 * @since 7.1 044 */ 045 public static SimpleDateFormat formatter() { 046 return new SimpleDateFormat(DATE_FORMAT); 047 } 048 049 /** 050 * Supports {@link Date}, {@link Calendar}, {@link Number} and {@link String} formatted as YYYY-MM-DD 051 * 052 * @param object Any object 053 * @return a date represented as number of milliseconds since january 1 1970 if the object is supported, null 054 * otherwise. 055 * @since 7.1 056 */ 057 public static Long objectToTimeMillis(Object object) { 058 Long timeValue = null; 059 if (object == null) { 060 return null; 061 } 062 if (object instanceof Date) { 063 timeValue = ((Date) object).getTime(); 064 } else if (object instanceof Calendar) { 065 timeValue = ((Calendar) object).getTimeInMillis(); 066 } else if (object instanceof Number) { 067 timeValue = ((Number) object).longValue(); 068 } else { 069 SimpleDateFormat dateParser = ConstraintUtils.formatter(); 070 try { 071 timeValue = dateParser.parse(object.toString()).getTime(); 072 } catch (ParseException e) { 073 return null; 074 } 075 } 076 return timeValue; 077 } 078 079 /** 080 * Supports any object which toString method return a numeric as String. 081 * 082 * @param object Any object 083 * @return a BigDecimal if the object represent a number, null otherwise. 084 * @since 7.1 085 */ 086 public static BigDecimal objectToBigDecimal(Object object) { 087 if (object == null) { 088 return null; 089 } 090 try { 091 return new BigDecimal(object.toString()); 092 } catch (NumberFormatException nfe) { 093 return null; 094 } 095 } 096 097 /** 098 * Supports any object which toString method return a positive numeric as String. 099 * 100 * @param object Any object 101 * @return a positive long value (rounded if needed) if the object represent a positive numeric, null otherwise. 102 * @since 7.1 103 */ 104 public static Long objectToPostiveLong(Object object) { 105 if (object == null) { 106 return null; 107 } 108 try { 109 Long result = Long.parseLong(object.toString()); 110 if (result >= 0) { 111 return result; 112 } else { 113 return null; 114 } 115 } catch (NumberFormatException nfe) { 116 return null; 117 } 118 } 119 120 @SuppressWarnings("unchecked") 121 public static <T extends Constraint> T getConstraint(Collection<Constraint> constraints, Class<T> constraintClass) { 122 for (Constraint constraint : constraints) { 123 if (constraint.getClass().equals(constraintClass)) { 124 return (T) constraint; 125 } 126 } 127 return null; 128 } 129 130}