OSDN Git Service

remove Comparable from Village.
authorOlyutorskii <olyutorskii@users.osdn.me>
Fri, 21 Feb 2020 17:31:33 +0000 (02:31 +0900)
committerOlyutorskii <olyutorskii@users.osdn.me>
Fri, 21 Feb 2020 17:31:33 +0000 (02:31 +0900)
src/main/java/jp/sfjp/jindolf/data/Village.java
src/main/java/jp/sfjp/jindolf/data/html/VillageListLoader.java
src/main/java/jp/sfjp/jindolf/data/html/VillageRecord.java

index eef9fbc..5e0fee2 100644 (file)
@@ -11,7 +11,6 @@ import java.awt.image.BufferedImage;
 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;
@@ -24,13 +23,10 @@ import jp.sourceforge.jindolf.corelib.VillageState;
 /**
  * いわゆる「村」。
  */
-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;
@@ -83,15 +79,6 @@ public class Village implements Comparable<Village> {
 
 
     /**
-     * 村同士を比較するためのComparatorを返す。
-     * @return Comparatorインスタンス
-     */
-    public static Comparator<Village> comparator(){
-        return VILLAGE_COMPARATOR;
-    }
-
-
-    /**
      * 所属する国を返す。
      * @return 村の所属する国(Land)
      */
@@ -536,49 +523,6 @@ public class Village implements Comparable<Village> {
 
     /**
      * {@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 村の名前
@@ -588,39 +532,4 @@ public class Village implements Comparable<Village> {
         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;
-        }
-
-    }
-
 }
index 14fe783..2695e49 100644 (file)
@@ -71,7 +71,7 @@ public final class VillageListLoader {
         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();
@@ -87,13 +87,9 @@ public final class VillageListLoader {
             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;
     }
 
@@ -105,7 +101,7 @@ public final class VillageListLoader {
      * <p>古国(wolf)の場合は村一覧にアクセスせずトップページのみ。
      * 古国以外で村建てをやめた国はトップページにアクセスしない。
      *
-     * <p>戻される村一覧リストは順不同で重複もありうる
+     * <p>戻される村一覧リストは順序づけられており重複はない
      *
      * @param land 国
      * @return 村一覧リスト
@@ -113,9 +109,6 @@ public final class VillageListLoader {
      */
     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();
@@ -126,31 +119,37 @@ public final class VillageListLoader {
 
         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;
     }
 
     /**
index 9716b7b..cfbf9b3 100644 (file)
@@ -12,12 +12,15 @@ import jp.sourceforge.jindolf.corelib.VillageState;
 /**
  * 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.
      *
@@ -34,6 +37,8 @@ class VillageRecord {
         this.fullVillageName = fullVillageName;
         this.villageStatus = villageStatus;
 
+        this.villageIdNum = Integer.parseInt(villageId);
+
         return;
     }
 
@@ -64,4 +69,45 @@ class VillageRecord {
         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);
+    }
+
 }