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