Clover coverage report - groovy - 1.0-beta-6
Coverage timestamp: Thu Jul 15 2004 13:18:22 BST
file stats: LOC: 200   Methods: 19
NCLOC: 117   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
GString.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  * $Id: GString.java,v 1.12 2004/05/04 02:54:44 spullara Exp $
 3   
  * 
 4   
  * Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
 5   
  * 
 6   
  * Redistribution and use of this software and associated documentation
 7   
  * ("Software"), with or without modification, are permitted provided that the
 8   
  * following conditions are met: 1. Redistributions of source code must retain
 9   
  * copyright statements and notices. Redistributions must also contain a copy
 10   
  * of this document. 2. Redistributions in binary form must reproduce the above
 11   
  * copyright notice, this list of conditions and the following disclaimer in
 12   
  * the documentation and/or other materials provided with the distribution. 3.
 13   
  * The name "groovy" must not be used to endorse or promote products derived
 14   
  * from this Software without prior written permission of The Codehaus. For
 15   
  * written permission, please contact info@codehaus.org. 4. Products derived
 16   
  * from this Software may not be called "groovy" nor may "groovy" appear in
 17   
  * their names without prior written permission of The Codehaus. "groovy" is a
 18   
  * registered trademark of The Codehaus. 5. Due credit should be given to The
 19   
  * Codehaus - http://groovy.codehaus.org/
 20   
  * 
 21   
  * THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS ``AS IS'' AND ANY
 22   
  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 23   
  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 24   
  * DISCLAIMED. IN NO EVENT SHALL THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR
 25   
  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 26   
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 27   
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 28   
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 29   
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 30   
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 31   
  * DAMAGE.
 32   
  *  
 33   
  */
 34   
 package groovy.lang;
 35   
 
 36   
 import java.io.IOException;
 37   
 import java.io.StringWriter;
 38   
 import java.io.Writer;
 39   
 import java.util.ArrayList;
 40   
 import java.util.Arrays;
 41   
 import java.util.List;
 42   
 
 43   
 import org.codehaus.groovy.runtime.InvokerHelper;
 44   
 
 45   
 /**
 46   
  * Represents a String which contains embedded values such as "hello there
 47   
  * ${user} how are you?" which can be evaluated lazily. Advanced users can
 48   
  * iterate over the text and values to perform special processing, such as for
 49   
  * performing SQL operations, the values can be substituted for ? and the
 50   
  * actual value objects can be bound to a JDBC statement. The lovely name of
 51   
  * this class was suggested by Jules Gosnell and was such a good idea, I
 52   
  * couldn't resist :)
 53   
  * 
 54   
  * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
 55   
  * @version $Revision: 1.12 $
 56   
  */
 57   
 public abstract class GString extends GroovyObjectSupport implements Comparable, CharSequence, Writable {
 58   
 
 59   
     private Object[] values;
 60   
 
 61  0
     public GString(Object values) {
 62  0
         this.values = (Object[]) values;
 63   
     }
 64   
 
 65  0
     public GString(Object[] values) {
 66  0
         this.values = values;
 67   
     }
 68   
 
 69   
     // will be static in an instance
 70   
     public abstract String[] getStrings();
 71   
 
 72   
     /**
 73   
      * Overloaded to implement duck typing for Strings 
 74   
      * so that any method that can't be evaluated on this
 75   
      * object will be forwarded to the toString() object instead.
 76   
      */
 77  0
     public Object invokeMethod(String name, Object args) {
 78  0
         try {
 79  0
             return super.invokeMethod(name, args);
 80   
         }
 81   
         catch (MissingMethodException e) {
 82   
             // lets try invoke the method on the real String
 83  0
             return InvokerHelper.invokeMethod(toString(), name, args);
 84   
         }
 85   
     }
 86   
 
 87  0
     public Object[] getValues() {
 88  0
         return values;
 89   
     }
 90   
 
 91  0
     public GString plus(GString that) {
 92  0
         List stringList = new ArrayList();
 93  0
         List valueList = new ArrayList();
 94   
 
 95  0
         stringList.addAll(Arrays.asList(getStrings()));
 96  0
         valueList.addAll(Arrays.asList(getValues()));
 97   
 
 98  0
         if (stringList.size() > valueList.size()) {
 99  0
             valueList.add("");
 100   
         }
 101   
 
 102  0
         stringList.addAll(Arrays.asList(that.getStrings()));
 103  0
         valueList.addAll(Arrays.asList(that.getValues()));
 104   
 
 105  0
         final String[] newStrings = new String[stringList.size()];
 106  0
         stringList.toArray(newStrings);
 107  0
         Object[] newValues = valueList.toArray();
 108   
 
 109  0
         return new GString(newValues) {
 110  0
             public String[] getStrings() {
 111  0
                 return newStrings;
 112   
             }
 113   
         };
 114   
     }
 115   
 
 116  0
     public GString plus(String that) {
 117  0
         String[] currentStrings = getStrings();
 118  0
         String[] newStrings = null;
 119  0
         Object[] newValues = null;
 120   
 
 121  0
         newStrings = new String[currentStrings.length + 1];
 122  0
         newValues = new Object[getValues().length + 1];
 123  0
         int lastIndex = currentStrings.length;
 124  0
         System.arraycopy(currentStrings, 0, newStrings, 0, lastIndex);
 125  0
         System.arraycopy(getValues(), 0, newValues, 0, getValues().length);
 126  0
         newStrings[lastIndex] = that;
 127  0
         newValues[getValues().length] = "";
 128   
 
 129  0
         final String[] finalStrings = newStrings;
 130  0
         return new GString(newValues) {
 131   
 
 132  0
             public String[] getStrings() {
 133  0
                 return finalStrings;
 134   
             }
 135   
         };
 136   
     }
 137   
 
 138  0
     public int getValueCount() {
 139  0
         return values.length;
 140   
     }
 141   
 
 142  0
     public Object getValue(int idx) {
 143  0
         return values[idx];
 144   
     }
 145   
 
 146  0
     public String toString() {
 147  0
         StringWriter buffer = new StringWriter();
 148  0
         try {
 149  0
             writeTo(buffer);
 150   
         }
 151   
         catch (IOException e) {
 152  0
             throw new StringWriterIOException(e);
 153   
         }
 154  0
         return buffer.toString();
 155   
     }
 156   
 
 157  0
     public Writer writeTo(Writer out) throws IOException {
 158  0
         String[] s = getStrings();
 159  0
         int numberOfValues = values.length;
 160  0
         for (int i = 0, size = s.length; i < size; i++) {
 161  0
             out.write(s[i]);
 162  0
             if (i < numberOfValues) {
 163  0
                 InvokerHelper.write(out, values[i]);
 164   
             }
 165   
         }
 166  0
         return out;
 167   
     }
 168   
 
 169  0
     public boolean equals(Object that) {
 170  0
         if (that instanceof GString) {
 171  0
             return equals((GString) that);
 172   
         }
 173  0
         return false;
 174   
     }
 175   
 
 176  0
     public boolean equals(GString that) {
 177  0
         return toString().equals(that.toString());
 178   
     }
 179   
 
 180  0
     public int hashCode() {
 181  0
         return 37 + toString().hashCode();
 182   
     }
 183   
 
 184  0
     public int compareTo(Object that) {
 185  0
         return toString().compareTo(that.toString());
 186   
     }
 187   
 
 188  0
     public char charAt(int index) {
 189  0
         return toString().charAt(index);
 190   
     }
 191   
 
 192  0
     public int length() {
 193  0
         return toString().length();
 194   
     }
 195   
 
 196  0
     public CharSequence subSequence(int start, int end) {
 197  0
         return toString().subSequence(start, end);
 198   
     }
 199   
 }
 200