File Source: ConfigSource.java

         /* 
    P/P   *  Method: com.dmdirc.config.ConfigSource__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  
    23  package com.dmdirc.config;
    24  
    25  import com.dmdirc.ui.messages.ColourManager;
    26  
    27  import java.awt.Color;
    28  import java.util.ArrayList;
    29  import java.util.Arrays;
    30  import java.util.List;
    31  
    32  /**
    33   * Defines methods to get options from a config source in various forms.
    34   *
    35   * @author chris
    36   */
         /* 
    P/P   *  Method: void com.dmdirc.config.ConfigSource()
          */
    37  public abstract class ConfigSource {
    38  
    39      /**
    40       * Retrieves the specified option.
    41       *
    42       * @param domain The domain of the option
    43       * @param option The name of the option
    44       * @return The value of the option
    45       */
    46      public abstract String getOption(final String domain, final String option);
    47  
    48      /**
    49       * Determines if this manager has the specified option.
    50       *
    51       * @param domain The domain of the option
    52       * @param option The name of the option
    53       * @return True iff the option exists, false otherwise.
    54       */
    55      protected abstract boolean hasOption(final String domain, final String option);
    56  
    57      /**
    58       * Determines if this manager has the specified String option.
    59       *
    60       * @param domain The domain of the option
    61       * @param option The name of the option
    62       * @since 0.6.3m1
    63       * @return True iff the option exists and is not empty, false otherwise
    64       */
    65      public boolean hasOptionString(final String domain, final String option) {
                 /* 
    P/P           *  Method: bool hasOptionString(String, String)
                  * 
                  *  Presumptions:
                  *    java.util.Map:get(...)@336 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    66          return hasOption(domain, option) && !getOption(domain, option).isEmpty();
    67      }
    68  
    69      /**
    70       * Determines if this manager has the specified integer option.
    71       *
    72       * @param domain The domain of the option
    73       * @param option The name of the option
    74       * @since 0.6.3m1
    75       * @return True iff the option exists and is parsable as an integer,
    76       * false otherwise.
    77       */
    78      public boolean hasOptionInt(final String domain, final String option) {
                 /* 
    P/P           *  Method: bool hasOptionInt(String, String)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    79          if (hasOption(domain, option)) {
    80              try {
    81                  getOptionInt(domain, option);
    82                  return true;
    83              } catch (NumberFormatException ex) {
    84                  // Do nothing
    85              }
    86          }
    87  
    88          return false;
    89      }
    90  
    91      /**
    92       * Determines if this manager has the specified character option.
    93       *
    94       * @param domain The domain of the option
    95       * @param option The name of the option
    96       * @since 0.6.3m1
    97       * @return True iff the option exists and is parsable as a char,
    98       * false otherwise.
    99       */
   100      public boolean hasOptionChar(final String domain, final String option) {
                 /* 
    P/P           *  Method: bool hasOptionChar(String, String)
                  * 
                  *  Presumptions:
                  *    java.util.Map:get(...)@336 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   101          return hasOption(domain, option) && !getOption(domain, option).isEmpty();
   102      }
   103  
   104      /**
   105       * Determines if this manager has the specified colour option.
   106       *
   107       * @param domain The domain of the option
   108       * @param option The name of the option
   109       * @since 0.6.3m1
   110       * @return True iff the option exists and is parsable as a colour,
   111       * false otherwise.
   112       */
   113      public boolean hasOptionColour(final String domain, final String option) {
                 /* 
    P/P           *  Method: bool hasOptionColour(String, String)
                  * 
                  *  Preconditions:
                  *    this.sources != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   114          return getOptionColour(domain, option) != null;
   115      }
   116  
   117      /**
   118       * Retrieves the specified option as a character.
   119       *
   120       * @param domain The domain of the option
   121       * @param option The name of the option
   122       * @return The value of the option
   123       */
   124      public char getOptionChar(final String domain, final String option) {
                 /* 
    P/P           *  Method: char getOptionChar(String, String)
                  * 
                  *  Presumptions:
                  *    java.util.Map:get(...)@336 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   125          return getOption(domain, option).charAt(0);
   126      }
   127      
   128      /**
   129       * Retrieves a colour representation of the specified option.
   130       *
   131       * @param domain The domain of the option
   132       * @param option The name of the option
   133       * @param fallbacks An ordered array of further domains and options
   134       * (in pairs) to try if the specified domain/option isn't found
   135       * @return The colour representation of the option
   136       * @since 0.6.3m1
   137       */
   138      public Color getOptionColour(final String domain, final String option,
   139              final String ... fallbacks) {
   140          String value;
   141  
                 /* 
    P/P           *  Method: Color getOptionColour(String, String, String[])
                  * 
                  *  Preconditions:
                  *    this.sources != null
                  *    (soft) fallbacks != null
                  *    (soft) fallbacks.length <= 232-1
                  *    (soft) init'ed(fallbacks[0])
                  *    (soft) init'ed(fallbacks[1])
                  * 
                  *  Presumptions:
                  *    java.util.Arrays:copyOfRange(...).length@144 <= 232-1
                  *    java.util.Arrays:copyOfRange(...)@144 != null
                  *    java.util.Map:get(...)@336 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    java.lang.String:startsWith(...)@142: {0}, {1}
                  */
   142          if (!hasOption(domain, option) || (value = getOption(domain, option))
   143                  .startsWith("false:")) {
   144              return fallbacks.length >= 2 ? getOptionColour(fallbacks[0], fallbacks[1],
   145                      Arrays.copyOfRange(fallbacks, 2, fallbacks.length)) : null;
   146          }
   147          return ColourManager.parseColour(value.startsWith("true:")
   148                  ? value.substring(5) : value, null);
   149      }
   150  
   151      /**
   152       * Retrieves a boolean representation of the specified option.
   153       *
   154       * @param domain The domain of the option
   155       * @param option The name of the option
   156       * @return The boolean representation of the option
   157       */
   158      public boolean getOptionBool(final String domain, final String option) {
                 /* 
    P/P           *  Method: bool getOptionBool(String, String)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   159          return Boolean.parseBoolean(getOption(domain, option));
   160      }
   161  
   162      /**
   163       * Retrieves a list representation of the specified option.
   164       *
   165       * @param domain The domain of the option
   166       * @param option The name of the option
   167       * @param trimEmpty Whether or not to trim empty lines
   168       * @return The list representation of the option
   169       */
   170      public List<String> getOptionList(final String domain, final String option,
   171              final boolean trimEmpty) {
                 /* 
    P/P           *  Method: List getOptionList(String, String, bool)
                  * 
                  *  Presumptions:
                  *    java.util.Map:get(...)@336 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new ArrayList(getOptionList#1)
                  *    new ArrayList(getOptionList#1) num objects == 1
                  */
   172          final List<String> res = new ArrayList<String>();
   173  
   174          if (hasOption(domain, option)) {
   175              for (String line : getOption(domain, option).split("\n")) {
   176                  if (!line.isEmpty() || !trimEmpty) {
   177                      res.add(line);
   178                  }
   179              }
   180          }
   181  
   182          return res;
   183      }
   184  
   185      /**
   186       * Retrieves a list representation of the specified option, trimming empty
   187       * lines by default.
   188       *
   189       * @param domain The domain of the option
   190       * @param option The name of the option
   191       * @return The list representation of the option
   192       */
   193      public List<String> getOptionList(final String domain, final String option) {
                 /* 
    P/P           *  Method: List getOptionList(String, String)
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new ArrayList(getOptionList#1*)
                  *    new ArrayList(getOptionList#1*) num objects == 1
                  */
   194          return getOptionList(domain, option, true);
   195      }
   196  
   197      /**
   198       * Retrieves an integral representation of the specified option.
   199       *
   200       * @param domain The domain of the option
   201       * @param option The name of the option
   202       * @throws NumberFormatException If the setting can't be parsed
   203       * @return The integer representation of the option
   204       */
   205      public int getOptionInt(final String domain, final String option) {
                 /* 
    P/P           *  Method: int getOptionInt(String, String)
                  * 
                  *  Presumptions:
                  *    java.util.Map:get(...)@336 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   206          return Integer.parseInt(getOption(domain, option).trim());
   207      }
   208  
   209  }








SofCheck Inspector Build Version : 2.17854
ConfigSource.java 2009-Jun-25 01:54:24
ConfigSource.class 2009-Sep-02 17:04:11