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.cmdline.spi; 021 022 import java.util.StringTokenizer; 023 024 /** 025 * A typed string, this class should be extended to provide meta information about a string and provide type safety. 026 * This class is immutable and so should be its subclasses. 027 * 028 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a> 029 * @version $Revision$ 030 */ 031 public abstract class Value { 032 033 public static class Properties extends Value { 034 035 public Properties(String string) throws NullPointerException { 036 super(string); 037 } 038 039 public java.util.Properties getProperties() { 040 java.util.Properties props = new java.util.Properties(); 041 StringTokenizer tokenizer = new StringTokenizer(getString(), ";", false); 042 while(tokenizer.hasMoreTokens()){ 043 String token = tokenizer.nextToken(); 044 if(token.contains("=")) { 045 String key = token.substring(0, token.indexOf('=')); 046 String value = token.substring(token.indexOf('=') + 1, token.length()); 047 props.put(key, value); 048 } 049 } 050 return props; 051 } 052 } 053 054 /** . */ 055 private final String string; 056 057 /** 058 * The only constructors that accepts a string. 059 * 060 * @param string the string value 061 * @throws NullPointerException if the string is null 062 */ 063 public Value(String string) throws NullPointerException { 064 if (string == null) { 065 throw new NullPointerException("No null string accepted"); 066 } 067 this.string = string; 068 } 069 070 @Override 071 public int hashCode() { 072 return getClass().hashCode() ^ string.hashCode(); 073 } 074 075 @Override 076 public boolean equals(Object obj) { 077 if (obj == this) { 078 return true; 079 } else if (obj != null && obj.getClass().equals(getClass())) { 080 Value that = (Value)obj; 081 return string.equals(that.string); 082 } 083 return false; 084 } 085 086 /** 087 * Returns the string value. 088 * 089 * @return the string value 090 */ 091 public final String getString() { 092 return string; 093 } 094 095 @Override 096 public final String toString() { 097 return string; 098 } 099 }