Coverage Report - org.crosswire.jsword.passage.RestrictionType
 
Classes in this File Line Coverage Branch Coverage Complexity
RestrictionType
0%
0/19
0%
0/12
1.389
RestrictionType$1
0%
0/12
0%
0/2
1.389
RestrictionType$2
0%
0/26
N/A
1.389
 
 1  
 /**
 2  
  * Distribution License:
 3  
  * JSword is free software; you can redistribute it and/or modify it under
 4  
  * the terms of the GNU Lesser General Public License, version 2.1 or later
 5  
  * as published by the Free Software Foundation. This program is distributed
 6  
  * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 7  
  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 8  
  * See the GNU Lesser General Public License for more details.
 9  
  *
 10  
  * The License is available on the internet at:
 11  
  *      http://www.gnu.org/copyleft/lgpl.html
 12  
  * or by writing to:
 13  
  *      Free Software Foundation, Inc.
 14  
  *      59 Temple Place - Suite 330
 15  
  *      Boston, MA 02111-1307, USA
 16  
  *
 17  
  * © CrossWire Bible Society, 2005 - 2016
 18  
  *
 19  
  */
 20  
 package org.crosswire.jsword.passage;
 21  
 
 22  
 import org.crosswire.jsword.versification.BibleBook;
 23  
 import org.crosswire.jsword.versification.Versification;
 24  
 
 25  
 /**
 26  
  * Types of Passage Blurring Restrictions.
 27  
  * 
 28  
  * @see gnu.lgpl.License The GNU Lesser General Public License for details.
 29  
  * @author Joe Walker
 30  
  * @author DM Smith
 31  
  */
 32  0
 public enum RestrictionType {
 33  
     /**
 34  
      * There is no restriction on blurring.
 35  
      */
 36  0
     NONE {
 37  
         @Override
 38  
         public boolean isSameScope(Versification v11n, Verse start, Verse end) {
 39  0
             return true;
 40  
         }
 41  
 
 42  
         @Override
 43  
         public VerseRange blur(Versification v11n, VerseRange range, int blurDown, int blurUp) {
 44  0
             Verse start = v11n.subtract(range.getStart(), blurDown);
 45  0
             Verse end = v11n.add(range.getEnd(), blurUp);
 46  0
             return new VerseRange(v11n, start, end);
 47  
         }
 48  
 
 49  
         @Override
 50  
         public VerseRange blur(Versification v11n, Verse verse, int blurDown, int blurUp) {
 51  0
             Verse start = v11n.subtract(verse, blurDown);
 52  0
             Verse end = v11n.add(verse, blurUp);
 53  0
             return new VerseRange(v11n, start, end);
 54  
         }
 55  
 
 56  
         @Override
 57  
         public VerseRange toRange(Versification v11n, Verse verse, int count) {
 58  0
             Verse end = verse;
 59  0
             if (count > 1) {
 60  0
                 end = v11n.add(verse, count - 1);
 61  
             }
 62  0
             return new VerseRange(v11n, verse, end);
 63  
         }
 64  
     },
 65  
 
 66  
     /**
 67  
      * Blurring is restricted to the chapter
 68  
      */
 69  0
     CHAPTER {
 70  
         @Override
 71  
         public boolean isSameScope(Versification v11n, Verse start, Verse end) {
 72  0
             return v11n.isSameChapter(start, end);
 73  
         }
 74  
 
 75  
         @Override
 76  
         public VerseRange blur(Versification v11n, VerseRange range, int blurDown, int blurUp) {
 77  0
             Verse start = range.getStart();
 78  0
             BibleBook startBook = start.getBook();
 79  0
             int startChapter = start.getChapter();
 80  0
             int startVerse = start.getVerse() - blurDown;
 81  
 
 82  0
             Verse end = range.getEnd();
 83  0
             BibleBook endBook = end.getBook();
 84  0
             int endChapter = end.getChapter();
 85  0
             int endVerse = end.getVerse() + blurUp;
 86  
 
 87  0
             startVerse = Math.max(startVerse, 0);
 88  0
             endVerse = Math.min(endVerse, v11n.getLastVerse(endBook, endChapter));
 89  
 
 90  0
             Verse newStart = new Verse(v11n, startBook, startChapter, startVerse);
 91  0
             Verse newEnd = new Verse(v11n, endBook, endChapter, endVerse);
 92  0
             return new VerseRange(v11n, newStart, newEnd);
 93  
         }
 94  
 
 95  
         @Override
 96  
         public VerseRange blur(Versification v11n, Verse verse, int blurDown, int blurUp) {
 97  0
             BibleBook book = verse.getBook();
 98  0
             int chapter = verse.getChapter();
 99  0
             int startVerse = verse.getVerse() - blurDown;
 100  0
             int endVerse = verse.getVerse() + blurUp;
 101  
 
 102  0
             startVerse = Math.max(startVerse, 0);
 103  0
             endVerse = Math.min(endVerse, v11n.getLastVerse(book, chapter));
 104  
 
 105  0
             Verse start = new Verse(v11n, book, chapter, startVerse);
 106  0
             Verse end = new Verse(v11n, book, chapter, endVerse);
 107  0
             return new VerseRange(v11n, start, end);
 108  
         }
 109  
 
 110  
         @Override
 111  
         public VerseRange toRange(Versification v11n, Verse verse, int count) {
 112  0
             Verse end = v11n.add(verse, count - 1);
 113  0
             return new VerseRange(v11n, verse, end);
 114  
         }
 115  
     };
 116  
 
 117  
     /**
 118  
      * Are the two verses in the same scope.
 119  
      * 
 120  
      * @param v11n
 121  
      *            the versification to which this reference pertains
 122  
      * @param start
 123  
      *            the first verse
 124  
      * @param end
 125  
      *            the second verse
 126  
      * @return true if the two are in the same scope.
 127  
      */
 128  
     public abstract boolean isSameScope(Versification v11n, Verse start, Verse end);
 129  
 
 130  
     /**
 131  
      * Blur a verse range the specified amount. Since verse ranges are
 132  
      * immutable, it creates a new one.
 133  
      * 
 134  
      * @param v11n
 135  
      *            the versification to which this reference pertains
 136  
      * @param range
 137  
      * @param blurDown
 138  
      * @param blurUp
 139  
      * @return a verse range after blurring.
 140  
      */
 141  
     public abstract VerseRange blur(Versification v11n, VerseRange range, int blurDown, int blurUp);
 142  
 
 143  
     /**
 144  
      * Blur a verse the specified amount. Since verse are immutable and refer to
 145  
      * a single verse, it creates a verse range.
 146  
      * 
 147  
      * @param v11n
 148  
      *            the versification to which this reference pertains
 149  
      * @param verse
 150  
      * @param blurDown
 151  
      * @param blurUp
 152  
      * @return a verse range after blurring
 153  
      */
 154  
     public abstract VerseRange blur(Versification v11n, Verse verse, int blurDown, int blurUp);
 155  
 
 156  
     /**
 157  
      * Create a range from the verse having the specified number of verses.
 158  
      * 
 159  
      * @param v11n
 160  
      *            the versification to which this reference pertains
 161  
      * @param verse
 162  
      * @param count
 163  
      * @return a verse range created by extending a verse forward.
 164  
      */
 165  
     public abstract VerseRange toRange(Versification v11n, Verse verse, int count);
 166  
 
 167  
     /**
 168  
      * Get an integer representation for this RestrictionType
 169  
      * 
 170  
      * @return the integer representation
 171  
      */
 172  
     public int toInteger() {
 173  0
         return ordinal();
 174  
     }
 175  
 
 176  
     /**
 177  
      * Lookup method to convert from a String
 178  
      * @param name 
 179  
      * @return the matching restriction type
 180  
      */
 181  
     public static RestrictionType fromString(String name) {
 182  0
         for (RestrictionType v : values()) {
 183  0
             if (v.name().equalsIgnoreCase(name)) {
 184  0
                 return v;
 185  
             }
 186  
         }
 187  
 
 188  
         // cannot get here
 189  0
         assert false;
 190  0
         return null;
 191  
     }
 192  
 
 193  
     /**
 194  
      * Lookup method to convert from an integer
 195  
      * 
 196  
      * @param i 
 197  
      * @return the restriction type from its ordinal value
 198  
      */
 199  
     public static RestrictionType fromInteger(int i) {
 200  0
         for (RestrictionType v : values()) {
 201  0
             if (v.ordinal() == i) {
 202  0
                 return v;
 203  
             }
 204  
         }
 205  
 
 206  
         // cannot get here
 207  0
         assert false;
 208  0
         return null;
 209  
     }
 210  
 
 211  
     /**
 212  
      * The default Blur settings. This is used by config to set a default.
 213  
      * 
 214  
      * @param value
 215  
      *            The new default blur setting
 216  
      */
 217  
     public static void setBlurRestriction(int value) {
 218  0
         defaultBlurRestriction = RestrictionType.fromInteger(value);
 219  0
     }
 220  
 
 221  
     /**
 222  
      * The default Blur settings. This is used by config to manage a default
 223  
      * setting.
 224  
      * 
 225  
      * @return The current default blurRestriction setting
 226  
      */
 227  
     public static int getBlurRestriction() {
 228  0
         return getDefaultBlurRestriction().toInteger();
 229  
     }
 230  
 
 231  
     /**
 232  
      * The default Blur settings. This is used by BlurCommandWord
 233  
      * 
 234  
      * @return The current default blurRestriction setting
 235  
      */
 236  
     public static RestrictionType getDefaultBlurRestriction() {
 237  0
         return defaultBlurRestriction;
 238  
     }
 239  
 
 240  
     /**
 241  
      * A default restriction type for blurring.
 242  
      */
 243  0
     private static RestrictionType defaultBlurRestriction = RestrictionType.NONE;
 244  
 }