00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.metaserver;
00023
00024 import java.util.regex.Pattern;
00025 import org.jetbrains.annotations.NotNull;
00026 import org.jetbrains.annotations.Nullable;
00027
00032 public class MetaserverEntry implements Comparable<MetaserverEntry> {
00033
00037 @NotNull
00038 private static final Pattern HTML_TAG_MATCHER = Pattern.compile("<[^>]*>");
00039
00043 private final int updateSeconds;
00044
00048 @NotNull
00049 private final String hostname;
00050
00054 private final int players;
00055
00059 @NotNull
00060 private final String version;
00061
00065 @NotNull
00066 private final String comment;
00067
00071 private final long bytesIn;
00072
00076 private final long bytesOut;
00077
00081 private final int uptimeSeconds;
00082
00086 @NotNull
00087 private final String archBase;
00088
00092 @NotNull
00093 private final String mapBase;
00094
00098 @NotNull
00099 private final String codeBase;
00100
00115 public MetaserverEntry(final int updateSeconds, @NotNull final String hostname, final int players, @NotNull final String version, @NotNull final String comment, final long bytesIn, final long bytesOut, final int uptimeSeconds, @NotNull final String archBase, @NotNull final String mapBase, @NotNull final String codeBase) {
00116 this.updateSeconds = updateSeconds;
00117 this.hostname = hostname;
00118 this.players = players;
00119 this.version = version;
00120 this.comment = comment;
00121 this.bytesIn = bytesIn;
00122 this.bytesOut = bytesOut;
00123 this.uptimeSeconds = uptimeSeconds;
00124 this.archBase = archBase;
00125 this.mapBase = mapBase;
00126 this.codeBase = codeBase;
00127 }
00128
00133 public int getUpdateSeconds() {
00134 return updateSeconds;
00135 }
00136
00141 @NotNull
00142 public String getHostname() {
00143 return hostname;
00144 }
00145
00150 public int getPlayers() {
00151 return players;
00152 }
00153
00158 @NotNull
00159 public String getVersion() {
00160 return version;
00161 }
00162
00167 @NotNull
00168 public String getComment() {
00169 return comment;
00170 }
00171
00176 public long getBytesIn() {
00177 return bytesIn;
00178 }
00179
00184 public long getBytesOut() {
00185 return bytesOut;
00186 }
00187
00192 public int getUptimeSeconds() {
00193 return uptimeSeconds;
00194 }
00195
00200 @NotNull
00201 public String getArchBase() {
00202 return archBase;
00203 }
00204
00209 @NotNull
00210 public String getMapBase() {
00211 return mapBase;
00212 }
00213
00218 @NotNull
00219 public String getCodeBase() {
00220 return codeBase;
00221 }
00222
00226 @NotNull
00227 @Override
00228 public String toString() {
00229 return "Host:"+hostname+" Version:"+version+" Players:"+players+" Comment:"+comment;
00230 }
00231
00235 @Override
00236 public int compareTo(@NotNull final MetaserverEntry o) {
00237 return hostname.compareTo(o.hostname);
00238 }
00239
00243 @Override
00244 public int hashCode() {
00245 return hostname.hashCode();
00246 }
00247
00251 @Override
00252 public boolean equals(@Nullable final Object obj) {
00253 if (obj == null) {
00254 return false;
00255 }
00256 if (obj.getClass() != getClass()) {
00257 return false;
00258 }
00259 final MetaserverEntry m = (MetaserverEntry)obj;
00260 return m.hostname.equals(hostname);
00261 }
00262
00274 @NotNull
00275 public String format(@NotNull final String format) {
00276 final StringBuilder sb = new StringBuilder();
00277 final char[] formatChars = format.toCharArray();
00278 int i = 0;
00279 while (i < formatChars.length) {
00280 final char ch = formatChars[i++];
00281 if (ch != '%' || i >= formatChars.length) {
00282 sb.append(ch);
00283 } else {
00284 switch (formatChars[i++]) {
00285 case '%':
00286 sb.append('%');
00287 break;
00288
00289 case 'A':
00290 sb.append(archBase);
00291 break;
00292
00293 case 'C':
00294 sb.append(comment);
00295 break;
00296
00297 case 'D':
00298 sb.append(HTML_TAG_MATCHER.matcher(comment).replaceAll(" "));
00299 break;
00300
00301 case 'E':
00302 sb.append(codeBase);
00303 break;
00304
00305 case 'H':
00306 sb.append(hostname);
00307 break;
00308
00309 case 'I':
00310 sb.append(bytesIn);
00311 break;
00312
00313 case 'M':
00314 sb.append(mapBase);
00315 break;
00316
00317 case 'O':
00318 sb.append(bytesOut);
00319 break;
00320
00321 case 'P':
00322 sb.append(players);
00323 break;
00324
00325 case 'U':
00326 sb.append(updateSeconds);
00327 break;
00328
00329 case 'T':
00330 sb.append(uptimeSeconds);
00331 break;
00332
00333 case 'V':
00334 sb.append(version);
00335 break;
00336
00337 default:
00338 sb.append('%');
00339 sb.append(formatChars[i-1]);
00340 break;
00341 }
00342 }
00343 }
00344 return sb.toString();
00345 }
00346
00347 }