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
17package com.android.common.content;
18
19import java.util.Arrays;
20import java.util.HashMap;
21import java.util.Map;
22
23/**
24 * A convenience wrapper for a projection map.  Makes it easier to create and use projection maps.
25 */
26public class ProjectionMap extends HashMap<String, String> {
27
28    public static class Builder {
29
30        private ProjectionMap mMap = new ProjectionMap();
31
32        public Builder add(String column) {
33            mMap.putColumn(column, column);
34            return this;
35        }
36
37        public Builder add(String alias, String expression) {
38            mMap.putColumn(alias, expression + " AS " + alias);
39            return this;
40        }
41
42        public Builder addAll(String[] columns) {
43            for (String column : columns) {
44                add(column);
45            }
46            return this;
47        }
48
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
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
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     */
75    public String[] getColumnNames() {
76        return mColumns;
77    }
78
79    private void putColumn(String alias, String column) {
80        super.put(alias, column);
81    }
82
83    @Override
84    public String put(String key, String value) {
85        throw new UnsupportedOperationException();
86    }
87
88    @Override
89    public void putAll(Map<? extends String, ? extends String> map) {
90        throw new UnsupportedOperationException();
91    }
92}
93