001/*
002 * (C) Copyright 2006-2011 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 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.client.model;
020
021import java.util.Date;
022
023/**
024 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
025 */
026public class PropertiesHelper {
027
028    private PropertiesHelper() {
029    }
030
031    public static boolean isBlob(Object v) {
032        return v instanceof Blob;
033    }
034
035    public static boolean isMap(Object v) {
036        return v instanceof PropertyMap;
037    }
038
039    public static boolean isList(Object v) {
040        return v instanceof PropertyList;
041    }
042
043    public static boolean isScalar(Object v) {
044        return !isBlob(v) && !isList(v) && !isMap(v);
045    }
046
047    public static String getString(Object v, String defValue) {
048        if (v == null) {
049            return defValue;
050        }
051        if (v.getClass() == String.class) {
052            return v.toString();
053        }
054        throw new IllegalArgumentException("Property is not a scalar: " + v);
055    }
056
057    public static Boolean getBoolean(Object v, Boolean defValue) {
058        if (v == null) {
059            return defValue;
060        }
061        if (v.getClass() == String.class) {
062            return Boolean.valueOf(v.toString());
063        }
064        throw new IllegalArgumentException("Property is not a scalar: " + v);
065    }
066
067    public static Long getLong(Object v, Long defValue) {
068        if (v == null) {
069            return defValue;
070        }
071        if (v.getClass() == String.class) {
072            return Long.valueOf(v.toString());
073        }
074        throw new IllegalArgumentException("Property is not a scalar: " + v);
075    }
076
077    public static Double getDouble(Object v, Double defValue) {
078        if (v == null) {
079            return defValue;
080        }
081        if (v.getClass() == String.class) {
082            return Double.valueOf(v.toString());
083        }
084        throw new IllegalArgumentException("Property is not a scalar: " + v);
085    }
086
087    public static Date getDate(Object v, Date defValue) {
088        if (v == null) {
089            return defValue;
090        }
091        if (v.getClass() == String.class) {
092            return DateUtils.parseDate(v.toString());
093        } else {
094            return (Date) v;
095        }
096        // throw new IllegalArgumentException("Property is not a scalar: " + v);
097    }
098
099    public static PropertyList getList(Object v, PropertyList defValue) {
100        if (v == null) {
101            return defValue;
102        }
103        if (v instanceof PropertyList) {
104            return (PropertyList) v;
105        }
106        throw new IllegalArgumentException("Property is not a list: " + v);
107    }
108
109    public static PropertyMap getMap(Object v, PropertyMap defValue) {
110        if (v == null) {
111            return defValue;
112        }
113        if (v instanceof PropertyMap) {
114            return (PropertyMap) v;
115        }
116        throw new IllegalArgumentException("Property is not a map: " + v);
117    }
118
119}