001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nicolas Chapurlat <nchapurlat@nuxeo.com> 016 */ 017 018package org.nuxeo.ecm.core.schema.types.constraints; 019 020import java.math.BigDecimal; 021import java.text.ParseException; 022import java.text.SimpleDateFormat; 023import java.util.Calendar; 024import java.util.Collection; 025import java.util.Date; 026 027/** 028 * Some usefull utils for Constraint API. 029 * 030 * @since 7.1 031 */ 032public final class ConstraintUtils { 033 034 public static final String DATE_FORMAT = "yyyy-MM-dd"; 035 036 private ConstraintUtils() { 037 } 038 039 /** 040 * @return a date formatter xsd compliant : {@value #DATE_FORMAT} 041 * @since 7.1 042 */ 043 public static SimpleDateFormat formatter() { 044 return new SimpleDateFormat(DATE_FORMAT); 045 } 046 047 /** 048 * Supports {@link Date}, {@link Calendar}, {@link Number} and {@link String} formatted as YYYY-MM-DD 049 * 050 * @param object Any object 051 * @return a date represented as number of milliseconds since january 1 1970 if the object is supported, null 052 * otherwise. 053 * @since 7.1 054 */ 055 public static Long objectToTimeMillis(Object object) { 056 Long timeValue = null; 057 if (object == null) { 058 return null; 059 } 060 if (object instanceof Date) { 061 timeValue = ((Date) object).getTime(); 062 } else if (object instanceof Calendar) { 063 timeValue = ((Calendar) object).getTimeInMillis(); 064 } else if (object instanceof Number) { 065 timeValue = ((Number) object).longValue(); 066 } else { 067 SimpleDateFormat dateParser = ConstraintUtils.formatter(); 068 try { 069 timeValue = dateParser.parse(object.toString()).getTime(); 070 } catch (ParseException e) { 071 return null; 072 } 073 } 074 return timeValue; 075 } 076 077 /** 078 * Supports any object which toString method return a numeric as String. 079 * 080 * @param object Any object 081 * @return a BigDecimal if the object represent a number, null otherwise. 082 * @since 7.1 083 */ 084 public static BigDecimal objectToBigDecimal(Object object) { 085 if (object == null) { 086 return null; 087 } 088 try { 089 return new BigDecimal(object.toString()); 090 } catch (NumberFormatException nfe) { 091 return null; 092 } 093 } 094 095 /** 096 * Supports any object which toString method return a positive numeric as String. 097 * 098 * @param object Any object 099 * @return a positive long value (rounded if needed) if the object represent a positive numeric, null otherwise. 100 * @since 7.1 101 */ 102 public static Long objectToPostiveLong(Object object) { 103 if (object == null) { 104 return null; 105 } 106 try { 107 Long result = Long.parseLong(object.toString()); 108 if (result >= 0) { 109 return result; 110 } else { 111 return null; 112 } 113 } catch (NumberFormatException nfe) { 114 return null; 115 } 116 } 117 118 @SuppressWarnings("unchecked") 119 public static <T extends Constraint> T getConstraint(Collection<Constraint> constraints, Class<T> constraintClass) { 120 for (Constraint constraint : constraints) { 121 if (constraint.getClass().equals(constraintClass)) { 122 return (T) constraint; 123 } 124 } 125 return null; 126 } 127 128}