[Lutinutil-commits] r805 - in trunk/lutinutil/src/java/org/codelutin/util: . config
Author: tchemit Date: 2008-07-01 09:50:36 +0000 (Tue, 01 Jul 2008) New Revision: 805 Added: trunk/lutinutil/src/java/org/codelutin/util/config/ trunk/lutinutil/src/java/org/codelutin/util/config/Config.java trunk/lutinutil/src/java/org/codelutin/util/config/IdentityConfig.java trunk/lutinutil/src/java/org/codelutin/util/config/IdentityConfigProperty.java trunk/lutinutil/src/java/org/codelutin/util/config/Property.java trunk/lutinutil/src/java/org/codelutin/util/config/SimpleConfig.java trunk/lutinutil/src/java/org/codelutin/util/config/SimpleIdentityConfig.java Log: add config package Added: trunk/lutinutil/src/java/org/codelutin/util/config/Config.java =================================================================== --- trunk/lutinutil/src/java/org/codelutin/util/config/Config.java (rev 0) +++ trunk/lutinutil/src/java/org/codelutin/util/config/Config.java 2008-07-01 09:50:36 UTC (rev 805) @@ -0,0 +1,46 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program is + * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.util.config; + +import java.util.EnumMap; +import java.util.EnumSet; + +/** + * config contract based on a Enum to obtain properties + * + * @author chemit + */ +public interface Config<E extends Enum<E>> { + + EnumMap<E, Object> getProperties(); + + Object getProperty(E key); + + EnumSet<E> getUniverse(); + + void setProperties(EnumMap<E, Object> properties); + + void setProperty(E key, Object value); + + void copyFrom(Object src); + + void copyFrom(Object src, EnumSet<E> keys); + + void copyTo(Object dst); + + void copyTo(Object dst, EnumSet<E> keys); + + +} \ No newline at end of file Added: trunk/lutinutil/src/java/org/codelutin/util/config/IdentityConfig.java =================================================================== --- trunk/lutinutil/src/java/org/codelutin/util/config/IdentityConfig.java (rev 0) +++ trunk/lutinutil/src/java/org/codelutin/util/config/IdentityConfig.java 2008-07-01 09:50:36 UTC (rev 805) @@ -0,0 +1,37 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program is + * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.util.config; + + +/** + * Definition of an identity + * + * @author chemit + */ +public interface IdentityConfig extends Config<IdentityConfigProperty> { + + String getFirstName(); + + String getLastName(); + + String getEmail(); + + void setFirstName(String firstName); + + void setLastName(String lastName); + + void setEmail(String email); + +} \ No newline at end of file Added: trunk/lutinutil/src/java/org/codelutin/util/config/IdentityConfigProperty.java =================================================================== --- trunk/lutinutil/src/java/org/codelutin/util/config/IdentityConfigProperty.java (rev 0) +++ trunk/lutinutil/src/java/org/codelutin/util/config/IdentityConfigProperty.java 2008-07-01 09:50:36 UTC (rev 805) @@ -0,0 +1,45 @@ +/** + * ##% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program is + * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * ##% + */ +package org.codelutin.util.config; + +/** + * Enumaration of properties to be used in a {@link org.codelutin.util.config.IdentityConfig} + * + * @author chemit + * @see IdentityConfig + */ +public enum IdentityConfigProperty implements Property{ + + /** property <code>name</code>, first name of user */ + firstName(String.class), + + /** property <code>type</code>, lastname of user */ + lastName(String.class), + + /** property <code>email</code>, email of user */ + email(String.class); + + private Class<?> type; + + //TODO add locale + encoding + + public Class<?> getType() { + return type; + } + + IdentityConfigProperty(Class<?> type) { + this.type = type; + } +} \ No newline at end of file Added: trunk/lutinutil/src/java/org/codelutin/util/config/Property.java =================================================================== --- trunk/lutinutil/src/java/org/codelutin/util/config/Property.java (rev 0) +++ trunk/lutinutil/src/java/org/codelutin/util/config/Property.java 2008-07-01 09:50:36 UTC (rev 805) @@ -0,0 +1,21 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program is + * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.util.config; + +/** @author chemit */ +public interface Property { + + Class<?> getType(); +} Added: trunk/lutinutil/src/java/org/codelutin/util/config/SimpleConfig.java =================================================================== --- trunk/lutinutil/src/java/org/codelutin/util/config/SimpleConfig.java (rev 0) +++ trunk/lutinutil/src/java/org/codelutin/util/config/SimpleConfig.java 2008-07-01 09:50:36 UTC (rev 805) @@ -0,0 +1,131 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program is + * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.util.config; + +import org.apache.commons.beanutils.BeanUtils; +import org.apache.commons.beanutils.BeanUtilsBean; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.util.EnumMap; +import java.util.EnumSet; +import java.util.Map; + +/** + * simple config implementation based on a Enum to obtain properties + * + * @author chemit + */ +public class SimpleConfig<E extends Enum<E>> implements Config<E> { + + static protected final Log log = LogFactory.getLog(Config.class); + + protected EnumMap<E, Object> properties; + + protected final EnumSet<E> universe; + + protected final Class<E> klass; + + public SimpleConfig(Class<E> klass) { + this.klass = klass; + this.properties = new EnumMap<E, Object>(klass); + this.universe = java.util.EnumSet.allOf(klass); + } + + public SimpleConfig(Class<E> klass, EnumMap<E, Object> properties) { + this(klass); + setProperties(properties); + } + + public EnumMap<E, Object> getProperties() { + return properties.clone(); + } + + public Object getProperty(E key) { + return properties.get(key); + } + + public EnumSet<E> getUniverse() { + return universe; + } + + public void setProperties(EnumMap<E, Object> properties) { + for (Map.Entry<E, Object> entry : properties.entrySet()) { + setProperty(entry.getKey(), entry.getValue()); + } + } + + public void setProperty(E key, Object value) { + if (value == null) { + if (properties.containsKey(key)) { + // never keep a reference on a null value property + properties.remove(key); + } + } else { + log.info("key:" + key + ", value:" + value + ", type:" + value.getClass().getSimpleName()); + properties.put(key, value); + } + } + + @SuppressWarnings({"unchecked"}) + public void copyFrom(Object src) { + copyFrom(src, getUniverse()); + } + + public void copyFrom(Object src, EnumSet<E> keys) { + for (E key : keys) { + Object value = null; + try { + value = BeanUtilsBean.getInstance().getPropertyUtils().getProperty(src, key.name()); + setProperty(key, value); + } catch (Exception e) { + //TODO + log.warn("could not retreave property <" + key + ":" + value + "> on object " + src, e); + } + } + } + + public void copyTo(Object dst) { + copyTo(dst, getUniverse()); + } + + + public void copyTo(Object dst, EnumSet<E> keys) { + for (E key : keys) { + Object value = properties.get(key); + try { + log.info("save " + key); + BeanUtils.setProperty(dst, key.name(), value); + } catch (Exception e) { + //TODO + log.warn("could not set property <" + key + ":" + value + "> to object " + dst, e); + } + } + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (properties.isEmpty()) { + sb.append(" empty"); + } else { + for (Map.Entry<E, Object> entry : properties.entrySet()) { + sb.append(", ").append(entry.getKey()).append(':').append(entry.getValue()); + } + } + return super.toString() + '<' + sb.toString().substring(2) + '>'; + } + +} Added: trunk/lutinutil/src/java/org/codelutin/util/config/SimpleIdentityConfig.java =================================================================== --- trunk/lutinutil/src/java/org/codelutin/util/config/SimpleIdentityConfig.java (rev 0) +++ trunk/lutinutil/src/java/org/codelutin/util/config/SimpleIdentityConfig.java 2008-07-01 09:50:36 UTC (rev 805) @@ -0,0 +1,65 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program is + * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.util.config; + +import static org.codelutin.util.config.IdentityConfigProperty.email; +import static org.codelutin.util.config.IdentityConfigProperty.firstName; +import static org.codelutin.util.config.IdentityConfigProperty.lastName; + +import java.util.EnumMap; + +/** @author chemit */ +public class SimpleIdentityConfig extends SimpleConfig<IdentityConfigProperty> implements IdentityConfig { + + public SimpleIdentityConfig() { + super(IdentityConfigProperty.class); + } + + public SimpleIdentityConfig(String firstName, String lastName, String email) { + this(); + setFirstName(firstName); + setLastName(lastName); + setEmail(email); + } + + public SimpleIdentityConfig(EnumMap<IdentityConfigProperty, Object> properties) { + super(IdentityConfigProperty.class, properties); + } + + public String getFirstName() { + return (String) properties.get(firstName); + } + + public String getLastName() { + return (String) properties.get(lastName); + } + + public String getEmail() { + return (String) properties.get(email); + } + + public void setFirstName(String newFirstName) { + setProperty(firstName, newFirstName); + } + + public void setLastName(String newLastName) { + setProperty(lastName, newLastName); + } + + public void setEmail(String newEmail) { + setProperty(email, newEmail); + } + +}
participants (1)
-
tchemit@users.labs.libre-entreprise.org