Home | History | Annotate | Download | only in dex
      1 /*
      2  * Copyright (C) 2009 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 signature.converter.dex;
     18 
     19 import java.util.HashMap;
     20 import java.util.Map;
     21 
     22 import signature.model.impl.SigEnumConstant;
     23 import signature.model.impl.SigField;
     24 
     25 public class FieldPool {
     26 
     27     private Map<FieldKey, SigField> fieldStore;
     28     private Map<FieldKey, SigEnumConstant> constantStore;
     29 
     30     public FieldPool() {
     31         fieldStore = new HashMap<FieldKey, SigField>();
     32         constantStore = new HashMap<FieldKey, SigEnumConstant>();
     33     }
     34 
     35     private static class FieldKey {
     36         private final String qualifiedClassName;
     37         private final String fieldName;
     38 
     39         public FieldKey(String qualifiedClassName, String fieldName) {
     40             this.qualifiedClassName = qualifiedClassName;
     41             this.fieldName = fieldName;
     42         }
     43 
     44         @Override
     45         public int hashCode() {
     46             final int prime = 31;
     47             int result = prime * fieldName.hashCode();
     48             result = prime * result + qualifiedClassName.hashCode();
     49             return result;
     50         }
     51 
     52         @Override
     53         public boolean equals(Object obj) {
     54             FieldKey other = (FieldKey) obj;
     55             if (!fieldName.equals(other.fieldName)) {
     56                 return false;
     57             }
     58             if (!qualifiedClassName.equals(other.qualifiedClassName)) {
     59                 return false;
     60             }
     61             return true;
     62         }
     63     }
     64 
     65     public SigField getField(String qualifiedClassName, String fieldName) {
     66         FieldKey key = new FieldKey(qualifiedClassName, fieldName);
     67         SigField sigField = fieldStore.get(key);
     68         if (sigField == null) {
     69             sigField = new SigField(fieldName);
     70             fieldStore.put(key, sigField);
     71         }
     72         return sigField;
     73     }
     74 
     75     public SigEnumConstant getEnumConstant(String qualifiedName,
     76             String fieldName) {
     77         FieldKey key = new FieldKey(qualifiedName, fieldName);
     78         SigEnumConstant sigField = constantStore.get(key);
     79         if (sigField == null) {
     80             sigField = new SigEnumConstant(fieldName);
     81             constantStore.put(key, sigField);
     82         }
     83         return sigField;
     84     }
     85 
     86 }
     87