瀏覽代碼

javadoc
kosmetyczna refaktoryzacja AnsiFontScheme

Ranides Atterwim 13 年之前
父節點
當前提交
cc2d05f87f

+ 36 - 16
src/main/java/net/ranides/assira/text/format/AnsiFontArray.java

@@ -10,7 +10,7 @@ package net.ranides.assira.text.format;
 import java.awt.Font;
 import java.io.Serializable;
 import java.util.Arrays;
-import java.util.List;
+import java.util.Collection;
 import net.ranides.assira.collection.list.VirtualList;
 
 /**
@@ -21,7 +21,7 @@ public class AnsiFontArray extends AnsiFontScheme implements Serializable {
 
     private static final long serialVersionUID = 0x101L;
     
-    private Font[] array;
+    private final Font[] array;
     
     public AnsiFontArray(int size) {
         array = new Font[size];
@@ -52,18 +52,8 @@ public class AnsiFontArray extends AnsiFontScheme implements Serializable {
         array[index] = value;
     }
 
-    public int append(int delta) {
-        Font[] alloc = new Font[array.length + delta];
-        System.arraycopy(array, 0, alloc, 0, array.length);
-        array = alloc;
-        return array.length - 1;
-    }
-    
-    public int append() {
-        return append(1);
-    }
-    
-    public List<Font> values() {
+    @Override
+    public Collection<FontAttr> values() {
         return new ListView();
     }
     
@@ -71,15 +61,45 @@ public class AnsiFontArray extends AnsiFontScheme implements Serializable {
         return array.length;
     }
     
-    private final class ListView extends VirtualList<Font> {
+    private final class ListView extends VirtualList<FontAttr> {
         @Override
         public int size() {
             return AnsiFontArray.this.size();
         }
         @Override
-        public Font get(int index) {
+        public FontAttr get(int index) {
+            return new FontAttrView(index);
+        }
+    }
+    
+    @edu.umd.cs.findbugs.annotations.SuppressWarnings("SE_INNER_CLASS") // serializacja działa, więc nie kombinujmy
+    private final class FontAttrView implements FontAttr, Serializable {
+
+        private static final long serialVersionUID = 1L;
+        
+        private final int index;
+
+        public FontAttrView(int index) {
+            this.index = index;
+        }
+
+        @Override
+        public int index() {
+            return index;
+        }
+
+        @Override
+        public Font getFont() {
             return AnsiFontArray.this.getFont(index);
         }
+
+        @Override
+        public void setFont(Font value) {
+            AnsiFontArray.this.setFont(index, value);
+        }
+        
+        
+        
     }
     
 }

+ 53 - 19
src/main/java/net/ranides/assira/text/format/AnsiFontMap.java

@@ -9,8 +9,8 @@ package net.ranides.assira.text.format;
 
 import java.awt.Font;
 import java.io.Serializable;
+import java.util.Collection;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 
 /**
@@ -21,36 +21,70 @@ public class AnsiFontMap extends AnsiFontScheme implements Serializable {
 
     private static final long serialVersionUID = 0x101L;
     
-    final Map<Integer, Font> map;
-    
-    public AnsiFontMap(int capacity) {
-        map = new HashMap<Integer, Font>(capacity);
-    }
+    final Map<Integer, FontAttr> map;
     
     public AnsiFontMap() {
-        map = new HashMap<Integer, Font>();
+        map = new HashMap<Integer, FontAttr>();
     }
     
-    public AnsiFontMap(AnsiFontMap scheme) {
-        map = new HashMap<Integer, Font>(scheme.map);
-    }
-    
-    public AnsiFontMap(AnsiFontArray scheme) {
-        List<Font> values = scheme.values();
-        map = new HashMap<Integer, Font>( values.size() );
-        for(int i=0, n=values.size(); i<n; i++) {
-            map.put(i, values.get(i) );
+    public AnsiFontMap(AnsiFontScheme scheme) {
+        Collection<FontAttr> values = scheme.values();
+        map = new HashMap<Integer, FontAttr>( values.size() );
+        for(FontAttr value : values) {
+            map.put(value.index(), new FontAttrView(value));
         }
-     }
+    }
+
+    @Override
+    public Collection<FontAttr> values() {
+        return map.values();
+    }
 
     @Override
     public Font getFont(int index) {
-        return map.get( map.containsKey(index) ? index : 0);
+        return map.get( map.containsKey(index) ? index : 0).getFont();
     }
 
     @Override
     public void setFont(int index, Font value) {
-        map.put(index, value); 
+        if( !map.containsKey(index) ) { 
+            map.put(index, new FontAttrView(index, value)); 
+        } else {
+            map.get(index).setFont(value);
+        }
     }
     
+    private static final class FontAttrView implements FontAttr, Serializable {
+        
+        private static final long serialVersionUID = 1L;
+        
+        private final int index;
+        private Font font;
+
+        public FontAttrView(int index, Font font) {
+            this.index = index;
+            this.font = font;
+        }
+        
+        public FontAttrView(FontAttr item) {
+            this.index = item.index();
+            this.font = item.getFont();
+        }
+
+        @Override
+        public int index() {
+            return index;
+        }
+
+        @Override
+        public Font getFont() {
+            return font;
+        }
+
+        @Override
+        public void setFont(Font value) {
+            this.font = value;
+        }
+        
+    }
 }

+ 82 - 14
src/main/java/net/ranides/assira/text/format/AnsiFontScheme.java

@@ -6,8 +6,11 @@
  */
 package net.ranides.assira.text.format;
 
+import java.awt.Color;
 import java.awt.Font;
 import java.io.Serializable;
+import java.util.Collection;
+import net.ranides.assira.collection.SingleCollection;
 
 /**
  * Schemat przyporządkowujący graficzne reprezentacje czcionek do numerów, które
@@ -19,8 +22,14 @@ public abstract class AnsiFontScheme {
     /**
      * Pusty schemat kolorów. Zwraca {@code null} dla każdej wartości.
      */
-    public static final AnsiFontScheme EMPTY = new AnsiSchemeEmpty();
+    public static final AnsiFontScheme EMPTY = new AnsiSchemePlain(null);
 
+    /**
+     * Zwraca kolekcję wszystkich czcionek zapisanych w schemacie.
+     * @return 
+     */
+    public abstract Collection<FontAttr> values();
+    
     /**
      * Zwraca czcionkę o podanym indeksie.
      * @param index
@@ -53,6 +62,19 @@ public abstract class AnsiFontScheme {
 
 /* ************************************************************************** */
     
+    /**
+     * Kopiuje podany schemat czcionek
+     * @param scheme
+     * @return 
+     */
+    public static AnsiFontScheme copy(AnsiFontScheme scheme) {
+        if(scheme instanceof AnsiFontArray) {
+            return new AnsiFontArray((AnsiFontArray)scheme);
+        } else {
+            return new AnsiFontMap(scheme);
+        }
+    }
+    
     /**
      * Tworzy <i>scheme</i> o nieograniczonym rozmiarze, w którym można przypisać
      * wartość do dowolnego klucza (ciąg kluczy nie musi być sekwencją). Klucz
@@ -84,45 +106,91 @@ public abstract class AnsiFontScheme {
     public static AnsiFontScheme make(Font font) {
         return new AnsiSchemePlain(font);
     }
+    
+/* ************************************************************************** */
+    
+    
+    /**
+     * Element reprezentujący czcionkę przypisaną do danego indeksu w schemacie.
+     */
+    public interface FontAttr {
+        
+        /**
+         * Zwraca numer indeksu, z którym pozwiązana jest czcionka.
+         * @return 
+         */
+        int index();
+        
+        /**
+         * Zwraca czcionkę.
+         * @return
+         */
+        Font getFont();
+        
+        /**
+         * Ustawia czcionkę powiązaną z indeksem, do którego jest przyporządkowany element.
+         * Metoda modyfikuje schemat czcionek, który zwrócił element.
+         * @param value
+         */
+        void setFont(Font value);
+
+    }
+    
      
 /* ************************************************************************** */
     
+    
     private static final class AnsiSchemePlain extends AnsiFontScheme implements Serializable {
 
-        private static final long serialVersionUID = 0x101L;
+        private static final long serialVersionUID = 0x200L;
         
-        private Font font;
+        private FontAttrPlain font;
 
         public AnsiSchemePlain(Font font) {
-            this.font = font;
+            this.font = new FontAttrPlain(font);
         }
 
         @Override
         public Font getFont(int index) {
-            return font;
+            return font.getFont();
         }
 
         @Override
         public void setFont(int index, Font value) {
-            font = value;
+            font.setFont(value);
+        }
+
+        @Override
+        public Collection<FontAttr> values() {
+            return new SingleCollection<FontAttr>(font);
         }
 
     }
     
-    private static final class AnsiSchemeEmpty extends AnsiFontScheme implements Serializable {
+    private static final class FontAttrPlain implements FontAttr, Serializable {
+        
+        private static final long serialVersionUID = 0x200L;
+        
+        private Font font;
+        
+        public FontAttrPlain(Font font) {
+            this.font = font;
+        }
 
-        private static final long serialVersionUID = 0x101L;
-    
         @Override
-        public Font getFont(int index) {
-            return null;
+        public int index() {
+            return 0;
         }
 
         @Override
-        public void setFont(int index, Font value) {
-            /* do nothing */
+        public Font getFont() {
+            return font;
+        }
+
+        @Override
+        public void setFont(Font value) {
+            this.font = value;
         }
-    
     }
     
 }

+ 63 - 0
src/main/java/net/ranides/assira/text/format/AnsiStrings.java

@@ -16,10 +16,27 @@ public final class AnsiStrings {
         // utility class
     }
     
+    /**
+     * Sprawdza, czy oba obiekty reprezentują samo formatowanie uwzględniając 
+     * kolor,tło,czcionkę oraz publiczne (reprezentowane graficznie) atrybuty - 
+     * to znaczy używając maski {@link AnsiAttr#MASK_PUBLIC}
+     * @param left
+     * @param right
+     * @return
+     */
     public static boolean equals(AnsiAttr left, AnsiAttr right) {
         return equals(left, right, AnsiAttr.MASK_PUBLIC);
     }
     
+    /**
+     * Sprawdza, czy oba obiekty reprezentują to samo formatowanie uwzględniając 
+     * kolor,tło,czcionkę oraz podane atrybuty (np {@link AnsiAttr#BOLD}).
+     * @param left
+     * @param right
+     * @param flags
+     * @return
+     * @see AnsiAttr
+     */
     public static boolean equals(AnsiAttr left, AnsiAttr right, int flags) {
         if( left instanceof AnsiCharSequence && right instanceof AnsiCharSequence ) {
             return equals( (AnsiCharSequence)left, (AnsiCharSequence)right );
@@ -31,10 +48,28 @@ public final class AnsiStrings {
         return true;
     }
 
+    /**
+     * Sprawdza, czy obie sekwencje znaków zawierają dokładnie ten sam tekst
+     * uwzględniając formatowanie: kolor,tło,czcionkę oraz publiczne 
+     * (reprezentowane graficznie) atrybuty znaków - to znaczy używając maski 
+     * {@link AnsiAttr#MASK_PUBLIC}
+     * @param left
+     * @param right
+     * @return
+     */
     public static boolean equals(AnsiCharSequence left, AnsiCharSequence right) {
         return equals(left, right, AnsiAttr.MASK_PUBLIC);
     }
     
+    /**
+     * Sprawdza, czy obie sekwencje znaków zawierają dokładnie ten sam tekst
+     * uwzględniając formatowanie: kolor,tło,czcionkę oraz podane atrybuty 
+     * (np {@link AnsiAttr#BOLD}).
+     * @param left
+     * @param right
+     * @param flags
+     * @return
+     */
     public static boolean equals(AnsiCharSequence left, AnsiCharSequence right, int flags) {
         if( left == right ) { // NOPMD
             return true;
@@ -50,10 +85,30 @@ public final class AnsiStrings {
         return true;
     }
     
+    /**
+     * Sprawdza, czy podane sekwencje posiadają na podanej pozycji ten sam znak
+     * uwzględniając formatowanie: kolor,tło,czcionkę oraz publiczne 
+     * (reprezentowane graficznie) atrybuty znaków - to znaczy używając maski 
+     * {@link AnsiAttr#MASK_PUBLIC}
+     * @param left
+     * @param right
+     * @param index
+     * @return
+     */
     public static boolean equalsAt(AnsiCharSequence left, AnsiCharSequence right, int index) {
         return equalsAt(left, right, index, AnsiAttr.MASK_PUBLIC);
     }
     
+    /**
+     * Sprawdza, czy podane sekwencje posiadają na podanej pozycji ten sam znak
+     * uwzględniając formatowanie: kolor,tło,czcionkę oraz podane atrybuty 
+     * (np {@link AnsiAttr#BOLD}).
+     * @param left
+     * @param right
+     * @param index
+     * @param flags
+     * @return
+     */
     public static boolean equalsAt(AnsiCharSequence left, AnsiCharSequence right, int index, int flags) {
         if( left.charAt(index) != right.charAt(index) ) { return false; }
         if( left.getForeAt(index) != right.getForeAt(index) ) { return false; }
@@ -63,6 +118,14 @@ public final class AnsiStrings {
         return true;
     }
     
+    /**
+     * Ustawia w sekwencji docelowej znak o podanym indeksie pobrany z sekwencji 
+     * źródłowej - kopiując oprócz znaku również formatowanie (włącznie z wszystkimi,
+     * również niepublicznymi atrybutami).
+     * @param target
+     * @param source
+     * @param index
+     */
     public static void copy(AnsiString target, AnsiCharSequence source, int index) {
         target.setCharAt(index, source.charAt(index));
         target.setForeAt(index,   source.getForeAt(index));