Guest User

Untitled

a guest
May 28th, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.68 KB | None | 0 0
  1.  
  2. package com.skiwi.tessutils4j.data;
  3.  
  4. import com.skiwi.tessutils4j.Objects;
  5. import java.io.InvalidObjectException;
  6. import java.io.ObjectInputStream;
  7. import java.io.Serializable;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. /**
  12.  *
  13.  * @author Frank van Heeswijk
  14.  */
  15. public class Word implements Serializable {
  16.     private static final long serialVersionUID = 9084633893292833L;
  17.  
  18.     private final String word;
  19.     private final float confidence;
  20.     private final FontAttributes fontAttributes;
  21.     private final boolean fromDictionary;
  22.     private final boolean numeric;
  23.     private final Rectangle boundingBox;
  24.     private final Rectangle baseline;
  25.     private final List<Symbol> symbols;
  26.    
  27.     private Word(final String word, final float confidence, final FontAttributes fontAttributes, final boolean fromDictionary, final boolean numeric, final Rectangle boundingBox, final Rectangle baseline, final List<Symbol> symbols) {
  28.         this.word = Objects.requireNonNull(word, "word");
  29.         this.confidence = confidence;
  30.         this.fontAttributes = Objects.requireNonNull(fontAttributes, "fontAttributes");
  31.         this.fromDictionary = fromDictionary;
  32.         this.numeric = numeric;
  33.         this.boundingBox = Objects.requireNonNull(boundingBox, "boundingBox");
  34.         this.baseline = Objects.requireNonNull(baseline, "baseline");
  35.         this.symbols = Objects.requireNonNull(symbols, "symbols");
  36.     }
  37.  
  38.     public String getWord() {
  39.         return word;
  40.     }
  41.  
  42.     public float getConfidence() {
  43.         return confidence;
  44.     }
  45.  
  46.     public FontAttributes getFontAttributes() {
  47.         return fontAttributes;
  48.     }
  49.  
  50.     public boolean isFromDictionary() {
  51.         return fromDictionary;
  52.     }
  53.  
  54.     public boolean isNumeric() {
  55.         return numeric;
  56.     }
  57.    
  58.     public Rectangle getBoundingBox() {
  59.         return boundingBox;
  60.     }
  61.    
  62.     public Rectangle getBaseline() {
  63.         return baseline;
  64.     }
  65.  
  66.     public List<Symbol> getSymbols() {
  67.         return symbols;
  68.     }
  69.    
  70.     public static class WordBuilder {
  71.         private final String word;
  72.         private final float confidence;
  73.         private final FontAttributes fontAttributes;
  74.         private final boolean fromDictionary;
  75.         private final boolean numeric;
  76.         private final Rectangle boundingBox;
  77.         private final Rectangle baseline;
  78.         private final List<Symbol> symbols = new ArrayList<Symbol>();
  79.  
  80.         private WordBuilder(final String word, final float confidence, final FontAttributes fontAttributes, final boolean fromDictionary, final boolean numeric, final Rectangle boundingBox, final Rectangle baseline) {
  81.             this.word = word;
  82.             this.confidence = confidence;
  83.             this.fontAttributes = fontAttributes;
  84.             this.fromDictionary = fromDictionary;
  85.             this.numeric = numeric;
  86.             this.boundingBox = boundingBox;
  87.             this.baseline = baseline;
  88.         }
  89.        
  90.         public WordBuilder addSymbol(final Symbol symbol) {
  91.             symbols.add(Objects.requireNonNull(symbol, "symbol"));
  92.             return this;
  93.         }
  94.        
  95.         public Word build() {
  96.             return new Word(word, confidence, fontAttributes, fromDictionary, numeric, boundingBox, baseline, symbols);
  97.         }
  98.     }
  99.    
  100.     private Object writeReplace() {
  101.         return new SerializationProxy(this);
  102.     }
  103.    
  104.     private void readObject(final ObjectInputStream stream) throws InvalidObjectException {
  105.         throw new InvalidObjectException("Proxy required");
  106.     }
  107.    
  108.     private static class SerializationProxy implements Serializable {
  109.         private static final long serialVersionUID = 794943938877393932L;
  110.        
  111.         private final String word;
  112.         private final float confidence;
  113.         private final FontAttributes fontAttributes;
  114.         private final boolean fromDictionary;
  115.         private final boolean numeric;
  116.         private final Rectangle boundingBox;
  117.         private final Rectangle baseline;
  118.         private final List<Symbol> symbols;
  119.        
  120.         private SerializationProxy(final Word word) {
  121.             this.word = word.word;
  122.             this.confidence = word.confidence;
  123.             this.fontAttributes = word.fontAttributes;
  124.             this.fromDictionary = word.fromDictionary;
  125.             this.numeric = word.numeric;
  126.             this.boundingBox = word.boundingBox;
  127.             this.baseline = word.baseline;
  128.             this.symbols = word.symbols;
  129.         }
  130.        
  131.         private Object readResolve() {
  132.             return new Word(word, confidence, fontAttributes, fromDictionary, numeric, boundingBox, baseline, symbols);
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment