001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * bstefanescu 011 */ 012package org.nuxeo.ecm.automation.client.model; 013 014import java.util.Date; 015 016/** 017 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 018 */ 019public class PropertiesHelper { 020 021 private PropertiesHelper() { 022 } 023 024 public static boolean isBlob(Object v) { 025 return v instanceof Blob; 026 } 027 028 public static boolean isMap(Object v) { 029 return v instanceof PropertyMap; 030 } 031 032 public static boolean isList(Object v) { 033 return v instanceof PropertyList; 034 } 035 036 public static boolean isScalar(Object v) { 037 return !isBlob(v) && !isList(v) && !isMap(v); 038 } 039 040 public static String getString(Object v, String defValue) { 041 if (v == null) { 042 return defValue; 043 } 044 if (v.getClass() == String.class) { 045 return v.toString(); 046 } 047 throw new IllegalArgumentException("Property is not a scalar: " + v); 048 } 049 050 public static Boolean getBoolean(Object v, Boolean defValue) { 051 if (v == null) { 052 return defValue; 053 } 054 if (v.getClass() == String.class) { 055 return Boolean.valueOf(v.toString()); 056 } 057 throw new IllegalArgumentException("Property is not a scalar: " + v); 058 } 059 060 public static Long getLong(Object v, Long defValue) { 061 if (v == null) { 062 return defValue; 063 } 064 if (v.getClass() == String.class) { 065 return Long.valueOf(v.toString()); 066 } 067 throw new IllegalArgumentException("Property is not a scalar: " + v); 068 } 069 070 public static Double getDouble(Object v, Double defValue) { 071 if (v == null) { 072 return defValue; 073 } 074 if (v.getClass() == String.class) { 075 return Double.valueOf(v.toString()); 076 } 077 throw new IllegalArgumentException("Property is not a scalar: " + v); 078 } 079 080 public static Date getDate(Object v, Date defValue) { 081 if (v == null) { 082 return defValue; 083 } 084 if (v.getClass() == String.class) { 085 return DateUtils.parseDate(v.toString()); 086 } else { 087 return (Date) v; 088 } 089 // throw new IllegalArgumentException("Property is not a scalar: " + v); 090 } 091 092 public static PropertyList getList(Object v, PropertyList defValue) { 093 if (v == null) { 094 return defValue; 095 } 096 if (v instanceof PropertyList) { 097 return (PropertyList) v; 098 } 099 throw new IllegalArgumentException("Property is not a list: " + v); 100 } 101 102 public static PropertyMap getMap(Object v, PropertyMap defValue) { 103 if (v == null) { 104 return defValue; 105 } 106 if (v instanceof PropertyMap) { 107 return (PropertyMap) v; 108 } 109 throw new IllegalArgumentException("Property is not a map: " + v); 110 } 111 112}