001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.platform.lang;
005    
006    import graphlab.platform.attribute.AtomAttribute;
007    import graphlab.platform.StaticUtils;
008    
009    import java.util.Vector;
010    
011    /**
012     * an eXtended data type that you can set it and also get it,
013     * only if your value is in the predefined set.
014     *
015     * @see graphlab.platform.lang.SetValidator
016     * @see graphlab.platform.attribute.AtomAttribute
017     *
018     * @author Azin Azadi
019     */
020    public class ArrayX<T> extends SetValidator<T> implements AtomAttribute<T> {
021        public ArrayX(T initVal, T... x) {
022            super.addValidValues(x);
023            super.addValidValue(initVal);
024            setValue(initVal);
025        }
026    
027        private T value;
028    
029        public boolean setValue(T t) {
030            if (isValid(t)) {
031                value = t;
032                return true;
033            }
034            return false;
035        }
036    
037        public T getValue() {
038            return value;
039        }
040    
041        public String toString() {
042            return super.toString() + " #$%# " + value.getClass().getName() + " #$%# " + value;
043        }
044    
045    //      test for the fromString
046    //    public static void main(String[] args) {
047    //        ArrayX o = new ArrayX("a", "s", "d");
048    //        String x = o.toString();
049    //        System.out.println(x);
050    //
051    //        for (Object oo : fromString(x).getValidValues()) {
052    //            System.out.println(oo);
053    //        }
054    //    }
055    
056        public static ArrayX fromString(String s) {
057            String[] ss = s.split(" #\\$%# ");
058            Object last = null;
059            Vector set = new Vector();
060            int i = 0;
061            while (i < ss.length) {
062                last = StaticUtils.fromString(ss[i++], ss[i++]);
063                set.add(last);
064            }
065            return new ArrayX(last, set.toArray());
066        }
067    }