1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ide.common.rendering.api; 18 19 import com.android.layoutlib.api.IResourceValue; 20 import com.android.resources.ResourceType; 21 22 /** 23 * Represents an android resource with a name and a string value. 24 */ 25 @SuppressWarnings("deprecation") 26 public class ResourceValue extends ResourceReference implements IResourceValue { 27 private final ResourceType mType; 28 private String mValue = null; 29 ResourceValue(ResourceType type, String name, boolean isFramework)30 public ResourceValue(ResourceType type, String name, boolean isFramework) { 31 super(name, isFramework); 32 mType = type; 33 } 34 ResourceValue(ResourceType type, String name, String value, boolean isFramework)35 public ResourceValue(ResourceType type, String name, String value, boolean isFramework) { 36 super(name, isFramework); 37 mType = type; 38 mValue = value; 39 } 40 getResourceType()41 public ResourceType getResourceType() { 42 return mType; 43 } 44 45 /** 46 * Returns the type of the resource. For instance "drawable", "color", etc... 47 * @deprecated use {@link #getResourceType()} instead. 48 */ 49 @Override 50 @Deprecated getType()51 public String getType() { 52 return mType.getName(); 53 } 54 55 /** 56 * Returns the value of the resource, as defined in the XML. This can be <code>null</code> 57 */ 58 @Override getValue()59 public final String getValue() { 60 return mValue; 61 } 62 63 /** 64 * Sets the value of the resource. 65 * @param value the new value 66 */ setValue(String value)67 public void setValue(String value) { 68 mValue = value; 69 } 70 71 /** 72 * Sets the value from another resource. 73 * @param value the resource value 74 */ replaceWith(ResourceValue value)75 public void replaceWith(ResourceValue value) { 76 mValue = value.mValue; 77 } 78 79 @Override toString()80 public String toString() { 81 return "ResourceValue [" + mType + "/" + getName() + " = " + mValue //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 82 + " (framework:" + isFramework() + ")]"; //$NON-NLS-1$ //$NON-NLS-2$ 83 } 84 85 /* (non-Javadoc) 86 * @see java.lang.Object#hashCode() 87 */ 88 @Override hashCode()89 public int hashCode() { 90 final int prime = 31; 91 int result = super.hashCode(); 92 result = prime * result + ((mType == null) ? 0 : mType.hashCode()); 93 result = prime * result + ((mValue == null) ? 0 : mValue.hashCode()); 94 return result; 95 } 96 97 /* (non-Javadoc) 98 * @see java.lang.Object#equals(java.lang.Object) 99 */ 100 @Override equals(Object obj)101 public boolean equals(Object obj) { 102 if (this == obj) 103 return true; 104 if (!super.equals(obj)) 105 return false; 106 if (getClass() != obj.getClass()) 107 return false; 108 ResourceValue other = (ResourceValue) obj; 109 if (mType == null) { 110 if (other.mType != null) 111 return false; 112 } else if (!mType.equals(other.mType)) 113 return false; 114 if (mValue == null) { 115 if (other.mValue != null) 116 return false; 117 } else if (!mValue.equals(other.mValue)) 118 return false; 119 return true; 120 } 121 } 122