1/* 2 * [The "BSD licence"] 3 * Copyright (c) 2010 Ben Gruver (JesusFreke) 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 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29package org.jf.dexlib.EncodedValue; 30 31import org.jf.dexlib.Util.AnnotatedOutput; 32 33public class BooleanEncodedValue extends EncodedValue { 34 /** 35 * The dupliton values 36 */ 37 public static final BooleanEncodedValue TrueValue = new BooleanEncodedValue(true); 38 public static final BooleanEncodedValue FalseValue = new BooleanEncodedValue(false); 39 40 public final boolean value; 41 42 /** 43 * Constructs a new <code>BooleanEncodedValue</code> with the given value 44 * @param value The value 45 */ 46 private BooleanEncodedValue(boolean value) { 47 this.value = value; 48 } 49 50 /** 51 * Gets the <code>BooleanEncodedValue</code> for the given valueArg value. The high 3 bits of the first byte should 52 * be passed as the valueArg parameter 53 * @param valueArg The high 3 bits of the first byte of this encoded value 54 * @return the <code>BooleanEncodedValue</code> for the given valueArg value 55 */ 56 protected static BooleanEncodedValue getBooleanEncodedValue(byte valueArg) { 57 if (valueArg == 0) { 58 return FalseValue; 59 } else if (valueArg == 1) { 60 return TrueValue; 61 } 62 throw new RuntimeException("valueArg must be either 0 or 1"); 63 } 64 65 /** 66 * Gets the <code>BooleanEncodedValue</code> for the given boolean value 67 * @param value the boolean value 68 * @return the <code>BooleanEncodedValue</code> for the given boolean value 69 */ 70 public static BooleanEncodedValue getBooleanEncodedValue(boolean value) { 71 if (value) { 72 return TrueValue; 73 } 74 return FalseValue; 75 } 76 77 /** {@inheritDoc} */ 78 public void writeValue(AnnotatedOutput out) { 79 if (out.annotates()) { 80 out.annotate("value_type=" + ValueType.VALUE_BOOLEAN.name() + ",value=" + Boolean.toString(value)); 81 } 82 out.writeByte(ValueType.VALUE_BOOLEAN.value | ((value?1:0) << 5)); 83 } 84 85 /** {@inheritDoc} */ 86 public int placeValue(int offset) { 87 return offset + 1; 88 } 89 90 /** {@inheritDoc} */ 91 protected int compareValue(EncodedValue o) { 92 BooleanEncodedValue other = (BooleanEncodedValue)o; 93 if (value == other.value) 94 return 0; 95 if (value) 96 return 1; 97 return -1; 98 } 99 100 /** {@inheritDoc} */ 101 public ValueType getValueType() { 102 return ValueType.VALUE_BOOLEAN; 103 } 104 105 @Override 106 public int hashCode() { 107 return value?1:0; 108 } 109} 110