1 /*
2  * Copyright (C) 2010 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.common.content;
18 
19 import java.util.Arrays;
20 import java.util.HashMap;
21 import java.util.Map;
22 
23 /**
24  * A convenience wrapper for a projection map.  Makes it easier to create and use projection maps.
25  */
26 public class ProjectionMap extends HashMap<String, String> {
27 
28     public static class Builder {
29 
30         private ProjectionMap mMap = new ProjectionMap();
31 
add(String column)32         public Builder add(String column) {
33             mMap.putColumn(column, column);
34             return this;
35         }
36 
add(String alias, String expression)37         public Builder add(String alias, String expression) {
38             mMap.putColumn(alias, expression + " AS " + alias);
39             return this;
40         }
41 
addAll(String[] columns)42         public Builder addAll(String[] columns) {
43             for (String column : columns) {
44                 add(column);
45             }
46             return this;
47         }
48 
addAll(ProjectionMap map)49         public Builder addAll(ProjectionMap map) {
50             for (Map.Entry<String, String> entry : map.entrySet()) {
51                 mMap.putColumn(entry.getKey(), entry.getValue());
52             }
53             return this;
54         }
55 
build()56         public ProjectionMap build() {
57             String[] columns = new String[mMap.size()];
58             mMap.keySet().toArray(columns);
59             Arrays.sort(columns);
60             mMap.mColumns = columns;
61             return mMap;
62         }
63 
64     }
65 
66     private String[] mColumns;
67 
builder()68     public static Builder builder() {
69         return new Builder();
70     }
71 
72     /**
73      * Returns a sorted array of all column names in the projection map.
74      */
getColumnNames()75     public String[] getColumnNames() {
76         return mColumns;
77     }
78 
putColumn(String alias, String column)79     private void putColumn(String alias, String column) {
80         super.put(alias, column);
81     }
82 
83     @Override
put(String key, String value)84     public String put(String key, String value) {
85         throw new UnsupportedOperationException();
86     }
87 
88     @Override
putAll(Map<? extends String, ? extends String> map)89     public void putAll(Map<? extends String, ? extends String> map) {
90         throw new UnsupportedOperationException();
91     }
92 }
93