001    /*
002     * Copyright (C) 2010 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software 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     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.plugin;
021    
022    import java.util.Collections;
023    import java.util.HashMap;
024    import java.util.Map;
025    import java.util.concurrent.TimeUnit;
026    
027    /**
028     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
029     * @version $Revision$
030     */
031    public abstract class PropertyDescriptor<T> {
032    
033      /** . */
034      private static final Map<String, PropertyDescriptor<?>> INTERNAL_ALL = new HashMap<String, PropertyDescriptor<?>>();
035    
036      /** . */
037      public static final Map<String, PropertyDescriptor<?>> ALL = Collections.unmodifiableMap(INTERNAL_ALL);
038    
039      /** . */
040      public static final PropertyDescriptor<TimeUnit> VFS_REFRESH_UNIT = new PropertyDescriptor<TimeUnit>(TimeUnit.class, "vfs.refresh_unit", TimeUnit.SECONDS, "The refresh time unit") {
041        @Override
042        public TimeUnit doParse(String s) {
043          return TimeUnit.valueOf(s);
044        }
045      };
046    
047      /** . */
048      public static final PropertyDescriptor<Integer> VFS_REFRESH_PERIOD = new PropertyDescriptor<Integer>(Integer.class, "vfs.refresh_period", null, "The refresh rate period") {
049        @Override
050        public Integer doParse(String s) {
051          return Integer.parseInt(s);
052        }
053      };
054    
055      /** . */
056      public final Class<T> type;
057    
058      /** . */
059      public final String name;
060    
061      /** . */
062      public final T defaultValue;
063    
064      /** . */
065      public final String description;
066    
067      protected PropertyDescriptor(Class<T> type, String name, T defaultValue, String description) {
068        if (name == null) {
069          throw new AssertionError();
070        }
071        this.type = type;
072        this.name = name;
073        this.defaultValue = defaultValue;
074        this.description = description;
075    
076        //
077        INTERNAL_ALL.put(name, this);
078      }
079    
080      public final String getName() {
081        return name;
082      }
083    
084      public final String getDescription() {
085        return description;
086      }
087    
088      public final Class<T> getType() {
089        return type;
090      }
091    
092      public final T getDefaultValue() {
093        return defaultValue;
094      }
095    
096      public final T parse(String s) throws NullPointerException, IllegalArgumentException {
097        if (s == null) {
098          throw new NullPointerException("Cannot parse null property values");
099        }
100        try {
101          return doParse(s);
102        }
103        catch (Exception e) {
104          throw new IllegalArgumentException("Illegal property value " + s, e);
105        }
106      }
107    
108      public final Property<T> toProperty(String s) throws NullPointerException, IllegalArgumentException {
109        T value = parse(s);
110        return new Property<T>(this, value);
111      }
112    
113      protected abstract T doParse(String s);
114    }