import java.io.IOException;
import java.text.MessageFormat;
import java.util.Collections;
-import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
/**
* いわゆる「村」。
*/
-public class Village implements Comparable<Village> {
+public class Village{
private static final int GID_MIN = 3;
- private static final Comparator<Village> VILLAGE_COMPARATOR =
- new VillageComparator();
-
private final Land parentLand;
private final String villageID;
/**
- * 村同士を比較するためのComparatorを返す。
- * @return Comparatorインスタンス
- */
- public static Comparator<Village> comparator(){
- return VILLAGE_COMPARATOR;
- }
-
-
- /**
* 所属する国を返す。
* @return 村の所属する国(Land)
*/
/**
* {@inheritDoc}
- * 二つの村を順序付ける。
- * @param village {@inheritDoc}
- * @return {@inheritDoc}
- */
- @Override
- public int compareTo(Village village){
- int cmpResult = VILLAGE_COMPARATOR.compare(this, village);
- return cmpResult;
- }
-
- /**
- * {@inheritDoc}
- * 二つの村が等しいか調べる。
- * @param obj {@inheritDoc}
- * @return {@inheritDoc}
- */
- @Override
- public boolean equals(Object obj){
- if(obj == null) return false;
- if( ! (obj instanceof Village) ) return false;
- Village village = (Village) obj;
-
- if( getParentLand() != village.getParentLand() ) return false;
-
- int cmpResult = compareTo(village);
- if(cmpResult == 0) return true;
- return false;
- }
-
- /**
- * {@inheritDoc}
- * @return {@inheritDoc}
- */
- @Override
- public int hashCode(){
- int homeHash = getParentLand().hashCode();
- int vidHash = getVillageID().hashCode();
- int result = homeHash ^ vidHash;
- return result;
- }
-
- /**
- * {@inheritDoc}
* 村の文字列表現を返す。
* 村の名前と等しい。
* @return 村の名前
return getVillageFullName();
}
-
- /**
- * 村同士を比較するためのComparator。
- */
- private static class VillageComparator implements Comparator<Village> {
-
- /**
- * コンストラクタ。
- */
- public VillageComparator(){
- super();
- return;
- }
-
- /**
- * {@inheritDoc}
- * @param v1 {@inheritDoc}
- * @param v2 {@inheritDoc}
- * @return {@inheritDoc}
- */
- @Override
- public int compare(Village v1, Village v2){
- int v1Num;
- if(v1 == null) v1Num = Integer.MIN_VALUE;
- else v1Num = v1.getVillageIDNum();
-
- int v2Num;
- if(v2 == null) v2Num = Integer.MIN_VALUE;
- else v2Num = v2.getVillageIDNum();
-
- return v1Num - v2Num;
- }
-
- }
-
}
LandState landState = landDef.getLandState();
boolean isHistorical = landState == LandState.HISTORICAL;
- List<Village> vList = new ArrayList<>(records.size());
+ List<Village> result = new ArrayList<>(records.size());
for(VillageRecord record : records){
String id = record.getVillageId();
Village village = new Village(land, id, fullVillageName);
village.setState(status);
- vList.add(village);
+ result.add(village);
}
- // たまに同じ村が複数回出現するので注意!
- SortedSet<Village> uniq = new TreeSet<>(vList);
- List<Village> result = new ArrayList<>(uniq);
-
return result;
}
* <p>古国(wolf)の場合は村一覧にアクセスせずトップページのみ。
* 古国以外で村建てをやめた国はトップページにアクセスしない。
*
- * <p>戻される村一覧リストは順不同で重複もありうる。
+ * <p>戻される村一覧リストは順序づけられており重複はない。
*
* @param land 国
* @return 村一覧リスト
*/
private static List<VillageRecord> loadVillageRecords(Land land)
throws IOException{
- List<VillageRecord> totalList = new LinkedList<>();
- List<VillageRecord> recList;
-
LandDef landDef = land.getLandDef();
boolean isVanillaWolf = landDef.getLandId().equals(ID_VANILLAWOLF);
LandState state = landDef.getLandState();
ServerAccess server = land.getServerAccess();
+ List<VillageRecord> result = new LinkedList<>();
+
// トップページ
if(needTopPage){
- recList = EMPTY_LIST;
+ List<VillageRecord> recList = EMPTY_LIST;
HtmlSequence html = server.getHTMLTopPage();
try{
recList = parseVillageRecords(html);
}catch(HtmlParseException e){
LOGGER.log(Level.WARNING, "トップページを認識できない", e);
}
- totalList.addAll(recList);
+ result.addAll(recList);
}
// 村一覧ページ
if(hasVillageList){
- recList = EMPTY_LIST;
+ List<VillageRecord> recList = EMPTY_LIST;
HtmlSequence html = server.getHTMLLandList();
try{
recList = parseVillageRecords(html);
}catch(HtmlParseException e){
LOGGER.log(Level.WARNING, "村一覧ページを認識できない", e);
}
- totalList.addAll(recList);
+ result.addAll(recList);
}
- return totalList;
+ // 昇順ソートと重複排除処理。 重複例) B国116村
+ SortedSet<VillageRecord> uniq = new TreeSet<>(result);
+ result = new ArrayList<>(uniq);
+
+ return result;
}
/**
/**
* Village record on HTML.
*/
-class VillageRecord {
+class VillageRecord implements Comparable<VillageRecord>{
private final String villageId;
private final String fullVillageName;
private final VillageState villageStatus;
+ private final int villageIdNum;
+
+
/**
* Constructor.
*
this.fullVillageName = fullVillageName;
this.villageStatus = villageStatus;
+ this.villageIdNum = Integer.parseInt(villageId);
+
return;
}
return this.villageStatus;
}
+ /**
+ * {@inheritDoc}
+ *
+ * <p>村IDの自然数順に順序づける。
+ *
+ * @param o {@inheritDoc}
+ * @return {@inheritDoc}
+ */
+ @Override
+ public int compareTo(VillageRecord o) {
+ int result = this.villageIdNum - o.villageIdNum;
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @return {@inheritDoc}
+ */
+ @Override
+ public int hashCode() {
+ return this.villageId.hashCode();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @param obj {@inheritDoc}
+ * @return {@inheritDoc}
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if(this == obj) return true;
+ if(obj == null) return false;
+
+ if(! (obj instanceof VillageRecord)) return false;
+ VillageRecord other = (VillageRecord) obj;
+
+ return this.villageId.equals(other.villageId);
+ }
+
}