Crossfire JXClient, Trunk  R20561
MetaserverEntry.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.metaserver;
23 
24 import java.util.regex.Pattern;
25 import org.jetbrains.annotations.NotNull;
26 import org.jetbrains.annotations.Nullable;
27 
32 public class MetaserverEntry implements Comparable<MetaserverEntry> {
33 
37  @NotNull
38  private static final Pattern HTML_TAG_MATCHER = Pattern.compile("<[^>]*>");
39 
43  private final int updateSeconds;
44 
48  @NotNull
49  private final String hostname;
50 
54  private final int players;
55 
59  @NotNull
60  private final String version;
61 
65  @NotNull
66  private final String comment;
67 
71  private final long bytesIn;
72 
76  private final long bytesOut;
77 
81  private final int uptimeSeconds;
82 
86  @NotNull
87  private final String archBase;
88 
92  @NotNull
93  private final String mapBase;
94 
98  @NotNull
99  private final String codeBase;
100 
115  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) {
116  this.updateSeconds = updateSeconds;
117  this.hostname = hostname;
118  this.players = players;
119  this.version = version;
120  this.comment = comment;
121  this.bytesIn = bytesIn;
122  this.bytesOut = bytesOut;
123  this.uptimeSeconds = uptimeSeconds;
124  this.archBase = archBase;
125  this.mapBase = mapBase;
126  this.codeBase = codeBase;
127  }
128 
133  public int getUpdateSeconds() {
134  return updateSeconds;
135  }
136 
141  @NotNull
142  public String getHostname() {
143  return hostname;
144  }
145 
150  public int getPlayers() {
151  return players;
152  }
153 
158  @NotNull
159  public String getVersion() {
160  return version;
161  }
162 
167  @NotNull
168  public String getComment() {
169  return comment;
170  }
171 
176  public long getBytesIn() {
177  return bytesIn;
178  }
179 
184  public long getBytesOut() {
185  return bytesOut;
186  }
187 
192  public int getUptimeSeconds() {
193  return uptimeSeconds;
194  }
195 
200  @NotNull
201  public String getArchBase() {
202  return archBase;
203  }
204 
209  @NotNull
210  public String getMapBase() {
211  return mapBase;
212  }
213 
218  @NotNull
219  public String getCodeBase() {
220  return codeBase;
221  }
222 
226  @NotNull
227  @Override
228  public String toString() {
229  return "Host:"+hostname+" Version:"+version+" Players:"+players+" Comment:"+comment;
230  }
231 
235  @Override
236  public int compareTo(@NotNull final MetaserverEntry o) {
237  return hostname.compareTo(o.hostname);
238  }
239 
243  @Override
244  public int hashCode() {
245  return hostname.hashCode();
246  }
247 
251  @Override
252  public boolean equals(@Nullable final Object obj) {
253  if (obj == null) {
254  return false;
255  }
256  if (obj.getClass() != getClass()) {
257  return false;
258  }
259  final MetaserverEntry m = (MetaserverEntry)obj;
260  return m.hostname.equals(hostname);
261  }
262 
285  @NotNull
286  public String format(@NotNull final String format) {
287  final StringBuilder sb = new StringBuilder();
288  final char[] formatChars = format.toCharArray();
289  int i = 0;
290  while (i < formatChars.length) {
291  final char ch = formatChars[i++];
292  if (ch != '%' || i >= formatChars.length) {
293  sb.append(ch);
294  } else {
295  switch (formatChars[i++]) {
296  case '%':
297  sb.append('%');
298  break;
299 
300  case 'A':
301  sb.append(archBase);
302  break;
303 
304  case 'C':
305  sb.append(comment);
306  break;
307 
308  case 'D':
309  sb.append(HTML_TAG_MATCHER.matcher(comment).replaceAll(" "));
310  break;
311 
312  case 'E':
313  sb.append(codeBase);
314  break;
315 
316  case 'H':
317  sb.append(hostname);
318  break;
319 
320  case 'I':
321  sb.append(bytesIn);
322  break;
323 
324  case 'M':
325  sb.append(mapBase);
326  break;
327 
328  case 'O':
329  sb.append(bytesOut);
330  break;
331 
332  case 'P':
333  sb.append(players);
334  break;
335 
336  case 'U':
337  sb.append(updateSeconds);
338  break;
339 
340  case 'T':
341  sb.append(uptimeSeconds);
342  break;
343 
344  case 'V':
345  sb.append(version);
346  break;
347 
348  default:
349  sb.append('%');
350  sb.append(formatChars[i-1]);
351  break;
352  }
353  }
354  }
355  return sb.toString();
356  }
357 
358 }
final String archBase
The server&#39;s arch base.
String format(@NotNull final String format)
Returns a formatted string using the given format.
final int uptimeSeconds
The server&#39;s uptime in seconds.
String getMapBase()
Returns the server&#39;s map base.
int getUptimeSeconds()
Returns the server&#39;s uptime in seconds.
final long bytesIn
The number of bytes received.
final String version
Returns the server version.
long getBytesOut()
Returns the number of bytes sent.
Represents a response line from the metaserver.
static final Pattern HTML_TAG_MATCHER
Matches html tags.
int getUpdateSeconds()
Returns the time since last update in seconds.
String getArchBase()
Returns the server&#39;s arch base.
String getCodeBase()
Returns the server&#39;s code base.
final int updateSeconds
Time since last update in seconds.
final String codeBase
The server&#39;s code base.
long getBytesIn()
Returns the number of bytes received.
final String comment
Returns the server comment.
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)
Creates a new instance.