|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| UnaryOpProperty.java | 100% | 93.3% | 100% | 95.7% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.drjava.config; | |
| 38 | ||
| 39 | import edu.rice.cs.plt.lambda.Lambda; | |
| 40 | ||
| 41 | /** Class representing unary operations that can be inserted as variables in external processes. | |
| 42 | * @version $Id: UnaryOpProperty.java 5175 2010-01-20 08:46:32Z mgricken $ | |
| 43 | */ | |
| 44 | public class UnaryOpProperty<P,R> extends EagerProperty { | |
| 45 | /** Operation to perform. */ | |
| 46 | protected Lambda<P,R> _op; | |
| 47 | /** Operator name */ | |
| 48 | protected String _op1Name; | |
| 49 | /** Operator default */ | |
| 50 | protected String _op1Default; | |
| 51 | /** Lambda to turn a string into the operand. */ | |
| 52 | protected Lambda<String,P> _parse; | |
| 53 | /** Lambda to format the result. */ | |
| 54 | protected Lambda<R,String> _format; | |
| 55 | ||
| 56 | /** Create an eager property. */ | |
| 57 | 92 | public UnaryOpProperty(String name, |
| 58 | String help, | |
| 59 | Lambda<P,R> op, | |
| 60 | String op1Name, | |
| 61 | String op1Default, | |
| 62 | Lambda<String,P> parse, | |
| 63 | Lambda<R,String> format) { | |
| 64 | 92 | super(name, help); |
| 65 | 92 | _op = op; |
| 66 | 92 | _op1Name = op1Name; |
| 67 | 92 | _op1Default = op1Default; |
| 68 | 92 | _parse = parse; |
| 69 | 92 | _format = format; |
| 70 | 92 | resetAttributes(); |
| 71 | } | |
| 72 | ||
| 73 | /** Create an eager property. */ | |
| 74 | 92 | public UnaryOpProperty(String name, |
| 75 | String help, | |
| 76 | Lambda<P,R> op, | |
| 77 | Lambda<String,P> parse, | |
| 78 | Lambda<R,String> format) { | |
| 79 | 92 | this(name, help, op, "op", null, parse, format); |
| 80 | } | |
| 81 | ||
| 82 | /** Update the property so the value is current. | |
| 83 | * @param pm PropertyMaps used for substitution when replacing variables */ | |
| 84 | 9 | public void update(PropertyMaps pm) { |
| 85 | 9 | P op; |
| 86 | 9 | if (_attributes.get(_op1Name) == null) { |
| 87 | 2 | _value = "(" + _name + " Error...)"; |
| 88 | 2 | return; |
| 89 | } | |
| 90 | else { | |
| 91 | 7 | try { |
| 92 | 7 | op = _parse.value(_attributes.get(_op1Name)); |
| 93 | } | |
| 94 | catch(Exception e) { | |
| 95 | 0 | _value = "(" + _name + " Error...)"; |
| 96 | 0 | return; |
| 97 | } | |
| 98 | } | |
| 99 | 7 | _value = _format.value(_op.value(op)); |
| 100 | } | |
| 101 | ||
| 102 | 184 | public void resetAttributes() { |
| 103 | 184 | _attributes.clear(); |
| 104 | 184 | _attributes.put(_op1Name, _op1Default); |
| 105 | } | |
| 106 | ||
| 107 | /** Lambda to parse a String into a Double. */ | |
| 108 | public static final Lambda<String,Double> PARSE_DOUBLE = | |
| 109 | new Lambda<String,Double>() { | |
| 110 | 176 | public Double value(String s) { return new Double(s); } |
| 111 | }; | |
| 112 | ||
| 113 | /** Lambda to parse a String into a Boolean. */ | |
| 114 | public static final Lambda<String,Boolean> PARSE_BOOL = | |
| 115 | new Lambda<String,Boolean>() { | |
| 116 | 38 | public Boolean value(String s) { return new Boolean(s); } |
| 117 | }; | |
| 118 | ||
| 119 | /** Lambda to parse a String into a String. */ | |
| 120 | public static final Lambda<String,String> PARSE_STRING = | |
| 121 | new Lambda<String,String>() { | |
| 122 | 265 | public String value(String s) { return s; } |
| 123 | }; | |
| 124 | ||
| 125 | /** Formatter for Booleans. */ | |
| 126 | public static final Lambda<Boolean,String> FORMAT_BOOL = | |
| 127 | new Lambda<Boolean,String>() { | |
| 128 | 42 | public String value(Boolean b) { return b.toString().toLowerCase(); } |
| 129 | }; | |
| 130 | ||
| 131 | /** Formatter for Numbers. */ | |
| 132 | public static final Lambda<Double,String> FORMAT_DOUBLE = | |
| 133 | new Lambda<Double,String>() { | |
| 134 | 39 | public String value(Double d) { |
| 135 | 39 | String s = d.toString(); |
| 136 | 35 | while(s.endsWith("0")) { s = s.substring(0, s.length()-1); } |
| 137 | 35 | if (s.endsWith(".")) { s = s.substring(0, s.length()-1); } |
| 138 | 39 | return s; |
| 139 | } | |
| 140 | }; | |
| 141 | ||
| 142 | /** Formatter for Strings. */ | |
| 143 | public static final Lambda<String,String> FORMAT_STRING = | |
| 144 | new Lambda<String,String>() { | |
| 145 | 77 | public String value(String s) { return s; } |
| 146 | }; | |
| 147 | } |
|
||||||||||