ProjectionMap.java revision d2fda1ec4db8d261bf0f5ca6dc43f8c204edbd87
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(ProjectionMap map) { 43 for (Map.Entry<String, String> entry : map.entrySet()) { 44 mMap.putColumn(entry.getKey(), entry.getValue()); 45 } 46 return this; 47 } 48 49 public ProjectionMap build() { 50 String[] columns = new String[mMap.size()]; 51 mMap.keySet().toArray(columns); 52 Arrays.sort(columns); 53 mMap.mColumns = columns; 54 return mMap; 55 } 56 57 } 58 59 private String[] mColumns; 60 61 public static Builder builder() { 62 return new Builder(); 63 } 64 65 /** 66 * Returns a sorted array of all column names in the projection map. 67 */ 68 public String[] getColumnNames() { 69 return mColumns; 70 } 71 72 private void putColumn(String alias, String column) { 73 super.put(alias, column); 74 } 75 76 @Override 77 public String put(String key, String value) { 78 throw new UnsupportedOperationException(); 79 } 80 81 @Override 82 public void putAll(Map<? extends String, ? extends String> map) { 83 throw new UnsupportedOperationException(); 84 } 85} 86