001 /* 002 * Copyright (C) 2012 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 public abstract class PropertyDescriptor<T> { 028 029 public static PropertyDescriptor<String> create(String name, String defaultValue, String description) { 030 return new PropertyDescriptor<String>(String.class, name, defaultValue, description) { 031 @Override 032 protected String doParse(String s) throws Exception { 033 return s; 034 } 035 }; 036 } 037 038 public static PropertyDescriptor<Integer> create(String name, Integer defaultValue, String description) { 039 return new PropertyDescriptor<Integer>(Integer.class, name, defaultValue, description) { 040 @Override 041 protected Integer doParse(String s) throws Exception { 042 return Integer.parseInt(s); 043 } 044 }; 045 } 046 047 /** . */ 048 private static final Map<String, PropertyDescriptor<?>> INTERNAL_ALL = new HashMap<String, PropertyDescriptor<?>>(); 049 050 /** . */ 051 public static final Map<String, PropertyDescriptor<?>> ALL = Collections.unmodifiableMap(INTERNAL_ALL); 052 053 /** . */ 054 public static final PropertyDescriptor<TimeUnit> VFS_REFRESH_UNIT = new PropertyDescriptor<TimeUnit>(TimeUnit.class, "vfs.refresh_unit", TimeUnit.SECONDS, "The refresh time unit") { 055 @Override 056 public TimeUnit doParse(String s) { 057 return TimeUnit.valueOf(s); 058 } 059 }; 060 061 /** . */ 062 public static final PropertyDescriptor<Integer> VFS_REFRESH_PERIOD = PropertyDescriptor.create("vfs.refresh_period", (Integer)null, "The refresh rate period"); 063 064 /** . */ 065 public final Class<T> type; 066 067 /** . */ 068 public final String name; 069 070 /** . */ 071 public final T defaultValue; 072 073 /** . */ 074 public final String description; 075 076 /** 077 * Create a new property descriptor. 078 * 079 * @param type the property type 080 * @param name the property name 081 * @param defaultValue the default value 082 * @param description the description 083 * @throws NullPointerException if the type, name or description is null 084 */ 085 protected PropertyDescriptor(Class<T> type, String name, T defaultValue, String description) throws NullPointerException { 086 if (type == null) { 087 throw new NullPointerException("No null type accepted"); 088 } 089 if (name == null) { 090 throw new NullPointerException("No null name accepted"); 091 } 092 if (description == null) { 093 throw new NullPointerException("No null description accepted"); 094 } 095 096 this.type = type; 097 this.name = name; 098 this.defaultValue = defaultValue; 099 this.description = description; 100 101 // 102 INTERNAL_ALL.put(name, this); 103 } 104 105 public final String getName() { 106 return name; 107 } 108 109 public final String getDescription() { 110 return description; 111 } 112 113 public final Class<T> getType() { 114 return type; 115 } 116 117 public final T getDefaultValue() { 118 return defaultValue; 119 } 120 121 /** 122 * Parse a string representation of a value and returns the corresponding typed value. 123 * 124 * @param s the string to parse 125 * @return the corresponding value 126 * @throws NullPointerException if the argument is null 127 * @throws IllegalArgumentException if the string value cannot be parsed for some reason 128 */ 129 public final T parse(String s) throws NullPointerException, IllegalArgumentException { 130 if (s == null) { 131 throw new NullPointerException("Cannot parse null property values"); 132 } 133 try { 134 return doParse(s); 135 } 136 catch (Exception e) { 137 throw new IllegalArgumentException("Illegal property value " + s, e); 138 } 139 } 140 141 /** 142 * Parse a string representation of a value and returns the correspondig property value. 143 * 144 * @param s the string to parse 145 * @return the corresponding property 146 * @throws NullPointerException if the argument is null 147 * @throws IllegalArgumentException if the string value cannot be parsed for some reason 148 */ 149 public final Property<T> toProperty(String s) throws NullPointerException, IllegalArgumentException { 150 T value = parse(s); 151 return new Property<T>(this, value); 152 } 153 154 /** 155 * Implements the real parsing, the string argument must nto be null. The returned value must not be null 156 * instead an exception must be thrown. 157 * 158 * @param s the string to parse 159 * @return the related value 160 * @throws Exception any exception that would prevent parsing to hapen 161 */ 162 protected abstract T doParse(String s) throws Exception; 163 164 @Override 165 public final String toString() { 166 return "PropertyDescriptor[name=" + name + ",type=" + type.getName() + ",description=" + description + "]"; 167 } 168 }