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.util;
21  
22  import org.xnap.XNap;
23  
24  /***
25   * Provides a mapping between connection speed and common internet acces 
26   * hardware.
27   */
28  public class LinkType implements Comparable
29  {
30  
31      //--- Constant(s) ---
32  
33      /***
34       * Unknown link type.
35       */ 
36      public static final LinkType UNKNOWN = new LinkType(XNap.tr("Unknown"), 0);
37  
38      /***
39       * The modem link.
40       */
41      public static final LinkType MODEM = new LinkType(XNap.tr("Modem"), 56);
42  
43      /***
44       * The ISDN link.
45       */
46      public static final LinkType ISDN = new LinkType(XNap.tr("ISDN"), 64);
47  
48      /***
49       * The cable link.
50       */
51      public static final LinkType CABLE = new LinkType(XNap.tr("Cable"), 500);
52  
53      /***
54       * The DSL link.
55       */ 
56      public static final LinkType DSL = new LinkType(XNap.tr("DSL"), 768);
57  
58      /***
59       * The T3 link.
60       */
61      public static final LinkType T3 = new LinkType(XNap.tr("T3"), 5000);
62  
63      /***
64       * The local area network link.
65       */
66      public static final LinkType LAN 
67  		= new LinkType(XNap.tr("Local Network"), 10000);
68  
69      /***
70       * An array of common link types.
71       */
72      public static final LinkType[] COMMON_TYPES = {
73  		UNKNOWN, MODEM, ISDN, CABLE, DSL, T3, LAN
74      };
75  
76      //--- Data field(s) ---
77  
78      private String type;
79      private int speed;
80  
81      //--- Constructor(s) ---
82  
83      /***
84       * Constructs a link speed object.
85       *
86       * @param type the name of the link type.
87       * @param speed the maximum speed of the link type in kbps
88       */
89      public LinkType(String type, int speed)
90      {
91  		this.type = type;
92  		this.speed = speed;
93      }
94  
95      //--- Method(s) ---
96  
97      public static int getIndexOfType(LinkType[] types, int speed)
98      {
99  		for (int i = 0; i < types.length; i++) {
100 			if (types[i].getSpeed() >= speed) {
101 				return i;
102 			}
103 		}
104 
105 		// return the fastest
106 		return types.length - 1;
107     }
108 
109 
110     /***
111      * Returns the type that is closest to <code>speed</code>
112      *
113      * @param speed the speed of the link type in kbps
114      */
115     public static LinkType getType(LinkType[] types, int speed)
116     {
117 		return types[getIndexOfType(types, speed)];
118     }
119 
120     /***
121      * Returns the common type that is closest to <code>speed</code>
122      *
123      * @param speed the speed of the link type in kbps
124      */
125     public static LinkType getType(int speed)
126     {
127 		return COMMON_TYPES[getIndexOfType(COMMON_TYPES, speed)];
128     }
129 
130     /***
131      * Returns the speed of the link type in kpbs.
132      */
133     public int getSpeed()
134     {
135 		return speed;
136     }
137 
138     public int compareTo(Object o)
139     {
140 		return speed - ((LinkType)o).speed;
141     }
142 
143     public String toString()
144     {
145 		return type;
146     }
147 
148 }