001/* ***** BEGIN LICENSE BLOCK ***** 002 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 003 * 004 * The contents of this file are subject to the Mozilla Public License Version 005 * 1.1 (the "License"); you may not use this file except in compliance with 006 * the License. You may obtain a copy of the License at 007 * http://www.mozilla.org/MPL/ 008 * 009 * Software distributed under the License is distributed on an "AS IS" basis, 010 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 011 * for the specific language governing rights and limitations under the 012 * License. 013 * 014 * The Original Code is part of dcm4che, an implementation of DICOM(TM) in 015 * Java(TM), hosted at https://github.com/gunterze/dcm4che. 016 * 017 * The Initial Developer of the Original Code is 018 * Agfa Healthcare. 019 * Portions created by the Initial Developer are Copyright (C) 2013 020 * the Initial Developer. All Rights Reserved. 021 * 022 * Contributor(s): 023 * See @authors listed below 024 * 025 * Alternatively, the contents of this file may be used under the terms of 026 * either the GNU General Public License Version 2 or later (the "GPL"), or 027 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 028 * in which case the provisions of the GPL or the LGPL are applicable instead 029 * of those above. If you wish to allow use of your version of this file only 030 * under the terms of either the GPL or the LGPL, and not to allow others to 031 * use your version of this file under the terms of the MPL, indicate your 032 * decision by deleting the provisions above and replace them with the notice 033 * and other provisions required by the GPL or the LGPL. If you do not delete 034 * the provisions above, a recipient may use your version of this file under 035 * the terms of any one of the MPL, the GPL or the LGPL. 036 * 037 * ***** END LICENSE BLOCK ***** */ 038 039package org.dcm4che3.util; 040 041import java.io.Serializable; 042import java.lang.reflect.InvocationTargetException; 043import java.util.Locale; 044 045/** 046 * @author Gunter Zeilinger <gunterze@gmail.com> 047 * 048 */ 049public class Property implements Serializable { 050 051 private static final long serialVersionUID = 6618989493749845502L; 052 053 private final String name; 054 private final Object value; 055 056 public Property(String name, Object value) { 057 if (name == null) 058 throw new NullPointerException("name"); 059 if (value == null) 060 throw new NullPointerException("value"); 061 062 if (!(value instanceof String 063 || value instanceof Boolean 064 || value instanceof Number)) 065 throw new IllegalArgumentException("value: " + value.getClass()); 066 067 this.name = name; 068 this.value = value; 069 } 070 071 public Property(String s) { 072 int endParamName = s.indexOf('='); 073 name = s.substring(0, endParamName); 074 value = valueOf(s.substring(endParamName+1)); 075 } 076 077 public final String getName() { 078 return name; 079 } 080 081 public final Object getValue() { 082 return value; 083 } 084 085 private static Object valueOf(String s) { 086 try { 087 return Double.valueOf(s); 088 } catch (NumberFormatException e) { 089 return s.equalsIgnoreCase("true") ? Boolean.TRUE : 090 s.equalsIgnoreCase("false") ? Boolean.FALSE 091 : s; 092 } 093 } 094 095 @Override 096 public int hashCode() { 097 return 31 * name.hashCode() + value.hashCode(); 098 } 099 100 @Override 101 public boolean equals(Object obj) { 102 if (this == obj) 103 return true; 104 if (obj == null) 105 return false; 106 if (getClass() != obj.getClass()) 107 return false; 108 109 Property other = (Property) obj; 110 return name.equals(other.name) 111 && value.equals(other.value); 112 } 113 114 @Override 115 public String toString() { 116 return name + '=' + value; 117 } 118 119 public void setAt(Object o) { 120 String setterName = "set" 121 + name.substring(0,1).toUpperCase(Locale.ENGLISH) 122 + name.substring(1); 123 try { 124 Class<?> clazz = o.getClass(); 125 if (value instanceof String) { 126 clazz.getMethod(setterName, String.class).invoke(o, value); 127 } else if (value instanceof Boolean) { 128 clazz.getMethod(setterName, boolean.class).invoke(o, value); 129 } else { // value instanceof Number 130 try { 131 clazz.getMethod(setterName, double.class) 132 .invoke(o, ((Number) value).doubleValue()); 133 } catch (NoSuchMethodException e) { 134 try { 135 clazz.getMethod(setterName, float.class) 136 .invoke(o, ((Number) value).floatValue()); 137 } catch (NoSuchMethodException e2) { 138 try { 139 clazz.getMethod(setterName, int.class) 140 .invoke(o, ((Number) value).intValue()); 141 } catch (NoSuchMethodException e3) { 142 throw e; 143 } 144 } 145 } 146 } 147 } catch (NoSuchMethodException e) { 148 throw new IllegalArgumentException(e.getMessage()); 149 } catch (IllegalAccessException e) { 150 throw new IllegalArgumentException(e.getMessage()); 151 } catch (InvocationTargetException e) { 152 throw new IllegalArgumentException(e.getMessage()); 153 } 154 } 155 156 public static Property[] valueOf(String[] ss) { 157 Property[] properties = new Property[ss.length]; 158 for (int i = 0; i < properties.length; i++) { 159 properties[i] = new Property(ss[i]); 160 } 161 return properties; 162 } 163}