Advertisement
Guest User

StringShifter.jave

a guest
Nov 17th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. /**
  2.  * Stores a string of text and provides a method for returning a version of the
  3.  * text with each character shifted by a specified number of characters to the
  4.  * right in the alphabet.
  5.  *
  6.  * @file StringShifter.java
  7.  * @author oz <oz@freqlabs.com>
  8.  * @date 2013-11-16
  9.  */
  10.  
  11. /*
  12. Copyright 2013 Freq Labs
  13.  
  14. Licensed under the Apache License, Version 2.0 (the "License");
  15. you may not use this file except in compliance with the License.
  16. You may obtain a copy of the License at
  17.  
  18.     http://www.apache.org/licenses/LICENSE-2.0
  19.  
  20. Unless required by applicable law or agreed to in writing, software
  21. distributed under the License is distributed on an "AS IS" BASIS,
  22. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. See the License for the specific language governing permissions and
  24. limitations under the License.
  25. */
  26.  
  27. public class StringShifter
  28. {
  29.     private String text;
  30.  
  31.     /**
  32.      * Performs bounds checking on the specified number.
  33.      *
  34.      * @param amount    the number to bounds check.
  35.      * @return          <code>true</code> if amount is between 1 and 25,
  36.      *                  inclusive; <code>false</code> otherwise.
  37.      */
  38.     private boolean isValidShift(int amount)
  39.     {
  40.         return 0 < amount && amount < 26;
  41.     }
  42.  
  43.     /**
  44.      * Shift a character n to the right in the alphabet, wrapping back to the
  45.      * beginning of the alphabet if the result exceeds base + 25.
  46.      *
  47.      * @param c     the character to shift.
  48.      * @param n     the amount of the shift.
  49.      * @param base  the character to wrap back to.
  50.      * @return      the shifted and wrapped value of c.
  51.      */
  52.     private char shiftFromBase(char c, int n, char base)
  53.     {
  54.         return (char)(((int)c + n - (int)base) % 26 + (int)base);
  55.     }
  56.  
  57.     /**
  58.      * Constructor for initializing the value of the text field.
  59.      *
  60.      * @param initialText   the initial value to assign the text field.
  61.      */
  62.     public StringShifter(String initialText)
  63.     {
  64.         text = initialText;
  65.     }
  66.  
  67.     /**
  68.      * Return a String with each character in the text field shifted to the
  69.      * right in the alphabet by the specified amount. If the shift amount is out
  70.      * of range, the original text is returned.
  71.      * Only alphabetical characters are shifted, everything else is unmodified.
  72.      *
  73.      * @param n         the number of characters to shift the text.
  74.      * @return          the shifted text if n is in bounds; the original text
  75.      *                  otherwise.
  76.      */
  77.     public String shift(int n)
  78.     {
  79.         String result_text;
  80.  
  81.         if ( isValidShift(n) )
  82.         {
  83.             result_text = "";
  84.  
  85.             for (char c : text.toCharArray())
  86.             {
  87.                 if ('A' <= c && c <= 'Z')
  88.                     result_text += shiftFromBase(c, n, 'A');
  89.                 else if ('a' <= c && c <= 'z')
  90.                     result_text += shiftFromBase(c, n, 'a');
  91.                 else
  92.                     result_text += c;
  93.             }
  94.         }
  95.         else
  96.         {
  97.             result_text = text;
  98.         }
  99.  
  100.         return result_text;
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement