Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/util/Parameter.java @ 8614

Last change on this file since 8614 was 6152, checked in by bfarka, 14 years ago

added ecj and custom statistics to communicate with the okb services #1441

File size: 4.8 KB
RevLine 
[6152]1/*
2  Copyright 2006 by Sean Luke
3  Licensed under the Academic Free License version 3.0
4  See the file "LICENSE" for more information
5*/
6
7
8package ec.util;
9import java.io.Serializable;
10
11/*
12 * Parameter.java
13 * Created: Sat Aug  7 12:06:49 1999
14 */
15
16/**
17 *
18 * <p>A Parameter is an object which the ParameterDatabase class
19 * uses as a key to associate with strings, forming a key-value pair.
20 * Parameters are designed to be hierarchical in nature, consisting
21 * of "path items" separated by a path separator.
22 * Parameters are created either from a single path item, from an array
23 * of path items, or both.  For example, a parameter with the path
24 * foo.bar.baz might be created from
25 * <tt>new Parameter(new String[] {"foo","bar","baz"})</tt>
26 *
27 * <p>Parameters are not mutable -- but once a parameter is created, path
28 * items may be pushed an popped from it, forming a new parameter.
29 * For example, if a parameter p consists of the path foo.bar.baz,
30 * p.pop() results in a new parameter whose path is foo.bar
31 * This pushing and popping isn't cheap, so be sparing.
32 *
33 * <p>Because this system internally uses "." as its path separator, you should
34 * not use that character in parts of the path that you provide; however
35 * if you need some other path separator, you can change the delimiter in
36 * the code trivially.
37 * In fact, you can create a new Parameter with a path foo.bar.baz simply
38 * by calling <tt>new Parameter("foo.bar.baz")</tt> but you'd better know
39 * what you're doing.
40 *
41 * <p>Additionally, parameters must not contain "#", "=", non-ascii values,
42 * or whitespace.  Yes, a parameter path item may be empty.
43 *
44 * @author Sean Luke
45 * @version 1.0
46 */
47
48
49public class Parameter implements Serializable
50    {
51    public String param;
52    public static final char delimiter = '.';
53
54    /** Creates a new parameter by joining the path items in s into a single path. */
55    public Parameter(String[] s) throws ec.util.BadParameterException
56        {
57        if (s.length==0)
58            throw new BadParameterException("Parameter created with length 0");
59        for (int x=0;x<s.length;x++)
60            {
61            if (s[x]==null)
62                throw new BadParameterException("Parameter created with null string");
63            if ( x == 0) param = s[x];
64            else param += ( delimiter + s[x] );
65            }
66        }
67
68
69    /** Creates a new parameter from the single path item in s. */
70    public Parameter (String s) throws BadParameterException
71        {
72        if (s==null)
73            throw new BadParameterException("Parameter created with null string");
74        param = s;
75        }
76
77
78    /** Creates a new parameter from the path item in s, plus the path items in s2.  s2 may be null or empty, but not s */
79    public Parameter(String s, String[] s2)
80        {
81        if (s==null)
82            throw new BadParameterException("Parameter created with null string");
83        param = s;
84        for (int x=0;x<s2.length;x++)
85            {
86            if (s2[x]==null)
87                throw new BadParameterException("Parameter created with null string");
88            else param += ( delimiter + s2[x] );
89            }
90        }
91
92
93
94    /** Returns a new parameter with s added to the end of the current path items. */
95    public Parameter push(String s)
96        {
97        if (s==null)
98            throw new BadParameterException("Parameter pushed with null string");
99        return new Parameter ( param + delimiter + s );
100        }
101
102
103    /** Returns a new parameter with the path items in s added to the end of the current path items. */
104    public Parameter push(String[] s)
105        {
106        return new Parameter(param,s);
107        }
108
109
110
111    /** Returns a new parameter with one path item popped off the end.  If this would result in a parameter with an empty collection of path items, null is returned. */
112    public Parameter pop()
113        {
114        int x = param.lastIndexOf(delimiter);
115        if (x==-1) // there's nothing left.
116            return null;
117        else return new Parameter(param.substring(0,x));
118        }
119
120    /** Returns a new parameter with n path items popped off the end.  If this would result in a parameter with an empty collection of path items, null is returned. */
121    public Parameter popn(int n)
122        {
123        String s = param;
124
125        for (int y=0;y<n;y++)
126            {
127            int x = s.lastIndexOf(delimiter);
128            if (x==-1) // there's nothing left
129                return null;
130            else s = param.substring(0,x);
131            }
132        return new Parameter(s);
133        }
134
135
136    /** Returns the path item at the far end of the parameter. */
137    public String top ()
138        {
139        int x = param.lastIndexOf(delimiter);
140        if (x==-1) return param;
141        else return param.substring(x+1);
142        }
143   
144    public String toString()
145        {
146        return param;
147        }
148
149    }
Note: See TracBrowser for help on using the repository browser.