View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.pkg;
21  
22  
23  /***
24   */
25  public class PackageToken extends AbstractToken
26  {
27  
28  	//--- Constant(s) ---
29  	
30  	public static final String[] COMPARE_MODES = new String[] {
31  		"=", ">=", ">>", "<=", "<<"
32  	};
33  
34  	//--- Data Field(s) ---
35  
36  	String compareMode;
37  	String name;
38  	String version;
39  
40  	//--- Constructor(s) ---
41  
42  	public PackageToken(String name, String compareMode, String version) 
43  		throws ParseException
44  	{
45  		this.name = name;
46  		this.compareMode = compareMode;
47  		this.version = version;
48  
49  		this.token = name;
50  		if (compareMode != null) {
51  			validate();
52  			this.token += "(" + compareMode + " " + version + ")";
53  		}		
54  	}
55  
56      //--- Method(s) ---
57  
58  	public boolean equalsVersion(int result)
59  	{
60  		if (compareMode.equals("=")) {
61  			return result == 0;
62  		}
63  		else if (compareMode.equals(">=")) {
64  			return result >= 0;
65  		}
66  		else if (compareMode.equals(">>")) {
67  			return result > 0;
68  		}
69  		else if (compareMode.equals("<=")) {
70  			return result <= 0;
71  		}
72  		else if (compareMode.equals("<<")) {
73  			return result < 0;
74  		}
75  
76  		throw new RuntimeException("invalid comparator");
77  	}
78  
79  	private void validate() throws ParseException
80  	{
81  		for (int i = 0; i < COMPARE_MODES.length; i++) {
82  			if (COMPARE_MODES[i].equals(compareMode)) {
83  				return;
84  			}
85  		}
86  		throw new ParseException("Invalid version comparator");
87  	}
88  
89  	//--- Inner Class(es) ---
90  
91  // 	public class CompareMode {
92  		
93  // 		private String mode;
94  
95  // 		CompareMode(String mode)
96  // 		{
97  // 			this.mode = mode;
98  // 		}
99  
100 // 		public String getMode()
101 // 		{
102 // 			return mode;
103 // 		}
104 
105 // 	}
106 
107 }
108