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 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.core.api.model.impl.primitives;
016
017import java.io.Serializable;
018
019import org.nuxeo.ecm.core.api.model.Property;
020import org.nuxeo.ecm.core.api.model.PropertyConversionException;
021import org.nuxeo.ecm.core.api.model.impl.ScalarProperty;
022import org.nuxeo.ecm.core.schema.types.Field;
023
024/**
025 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
026 */
027public class BooleanProperty extends ScalarProperty {
028
029    private static final long serialVersionUID = -6408890276716577303L;
030
031    public BooleanProperty(Property parent, Field field, int flags) {
032        super(parent, field, flags);
033    }
034
035    @Override
036    public boolean isNormalized(Object value) {
037        return value == null || value.getClass() == Boolean.class;
038    }
039
040    @Override
041    public Serializable normalize(Object value) throws PropertyConversionException {
042        if (isNormalized(value)) {
043            return (Serializable) value;
044        }
045        if (value.getClass() == String.class) {
046            String string = (String) value;
047            if (string.length() == 0) {
048                return null;
049            }
050            return Boolean.valueOf(value.toString());
051        }
052        if (value instanceof Number) {
053            return ((Number) value).intValue() == 0 ? Boolean.FALSE : Boolean.TRUE;
054        }
055        throw new PropertyConversionException(value.getClass(), Boolean.class);
056    }
057
058    @SuppressWarnings("unchecked")
059    @Override
060    public <T> T convertTo(Serializable value, Class<T> toType) throws PropertyConversionException {
061        if (value == null || Boolean.class == toType) {
062            return (T) value;
063        }
064        Boolean v = (Boolean) value;
065        if (toType == String.class) {
066            return (T) v.toString();
067        }
068        byte n = (byte) (v ? 1 : 0);
069        if (toType == Integer.class) {
070            return (T) new Integer(n);
071        }
072        if (toType == Long.class) {
073            return (T) new Long(n);
074        }
075        if (toType == Double.class) {
076            return (T) new Double(n);
077        }
078        if (toType == Float.class) {
079            return (T) new Float(n);
080        }
081        if (toType == Short.class) {
082            return (T) new Short(n);
083        }
084        if (toType == Byte.class) {
085            return (T) new Byte(n);
086        }
087        throw new PropertyConversionException(value.getClass(), toType);
088    }
089
090    @Override
091    public Object newInstance() {
092        return Boolean.FALSE;
093    }
094
095}