Coverage Report - joptsimple.util.KeyValuePair
 
Classes in this File Line Coverage Branch Coverage Complexity
KeyValuePair
95%
19/20
83%
10/12
2.4
 
 1  
 /*
 2  
  Copyright 2004-2008 Paul R. Holser, Jr.  All rights reserved.
 3  
  Licensed under the Academic Free License version 3.0
 4  
  */
 5  
 
 6  
 package joptsimple.util;
 7  
 
 8  
 import joptsimple.internal.Strings;
 9  
 
 10  
 /**
 11  
  * <p>A simple string key/string value pair.</p>
 12  
  *
 13  
  * <p>This is useful as an argument type for options whose values take on the form
 14  
  * <kbd>key=value</kbd>, such as JVM command line system properties.</p>
 15  
  *
 16  
  * @since 2.4
 17  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 18  
  * @version $Id: KeyValuePair.java,v 1.3 2008/04/01 22:09:41 pholser Exp $
 19  
  */
 20  
 public final class KeyValuePair {
 21  
     public final String key;
 22  
     public final String value;
 23  
 
 24  548
     private KeyValuePair( String key, String value ) {
 25  548
         this.key = key;
 26  548
         this.value = value;
 27  548
     }
 28  
 
 29  
     /**
 30  
      * Parses a string assumed to be of the form <kbd>key=value</kbd> into its parts.
 31  
      *
 32  
      * @param stringRepresentation key-value string
 33  
      * @return a key-value pair
 34  
      * @throws NullPointerException if <code>stringRepresentation</code> is
 35  
      * <code>null</code>
 36  
      */
 37  
     public static KeyValuePair valueOf( String stringRepresentation ) {
 38  550
         int equalsIndex = stringRepresentation.indexOf( '=' );
 39  548
         if ( equalsIndex == -1 )
 40  378
             return new KeyValuePair( stringRepresentation, Strings.EMPTY );
 41  
 
 42  170
         String aKey = stringRepresentation.substring( 0, equalsIndex );
 43  
         String aValue;
 44  
 
 45  170
         if ( equalsIndex == stringRepresentation.length() - 1 )
 46  2
             aValue = Strings.EMPTY;
 47  
         else
 48  168
             aValue = stringRepresentation.substring( equalsIndex + 1 );
 49  
 
 50  170
         return new KeyValuePair( aKey, aValue );
 51  
     }
 52  
 
 53  
     /**
 54  
      * {@inheritDoc}
 55  
      */
 56  
     public boolean equals( Object that ) {
 57  676
         if ( this == that )
 58  168
             return true;
 59  
 
 60  508
         if ( !( that instanceof KeyValuePair ) )
 61  0
             return false;
 62  
 
 63  508
         KeyValuePair other = (KeyValuePair) that;
 64  508
         return key.equals( other.key ) && value.equals( other.value );
 65  
     }
 66  
 
 67  
     /**
 68  
      * {@inheritDoc}
 69  
      */
 70  
     public int hashCode() {
 71  180
         return key.hashCode() ^ value.hashCode();
 72  
     }
 73  
 
 74  
     /**
 75  
      * {@inheritDoc}
 76  
      */
 77  
     public String toString() {
 78  2
         return key + '=' + value;
 79  
     }
 80  
 }