PageRenderTime 22ms CodeModel.GetById 10ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.2.0-rc0/src/java/org/apache/hcatalog/data/schema/HCatSchema.java

#
Java | 142 lines | 86 code | 21 blank | 35 comment | 12 complexity | 68d1257ed2d527131baefe9448447e80 MD5 | raw file
  1/*
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18package org.apache.hcatalog.data.schema;
 19
 20import java.io.Serializable;
 21import java.util.ArrayList;
 22import java.util.Collections;
 23import java.util.HashMap;
 24import java.util.List;
 25import java.util.Map;
 26
 27import org.apache.hcatalog.common.HCatException;
 28
 29/**
 30 * HCatSchema. This class is NOT thread-safe.
 31 */
 32
 33public class HCatSchema implements Serializable{
 34
 35    private static final long serialVersionUID = 1L;
 36
 37    private final List<HCatFieldSchema> fieldSchemas;
 38    private final Map<String,Integer> fieldPositionMap;
 39    private final List<String> fieldNames;
 40
 41    /**
 42     *
 43     * @param fieldSchemas is now owned by HCatSchema. Any subsequent modifications
 44     * on fieldSchemas won't get reflected in HCatSchema.  Each fieldSchema's name
 45     * in the list must be unique, otherwise throws IllegalArgumentException.
 46     */
 47    public HCatSchema(final List<HCatFieldSchema> fieldSchemas){
 48        this.fieldSchemas = new ArrayList<HCatFieldSchema>(fieldSchemas);
 49        int idx = 0;
 50        fieldPositionMap = new HashMap<String,Integer>();
 51        fieldNames = new ArrayList<String>();
 52        for (HCatFieldSchema field : fieldSchemas){
 53            if(field == null)
 54                throw new IllegalArgumentException("Field cannot be null");
 55
 56            String fieldName = field.getName();
 57            if(fieldPositionMap.containsKey(fieldName))
 58                throw new IllegalArgumentException("Field named " + fieldName +
 59                                                   " already exists");
 60            fieldPositionMap.put(fieldName, idx);
 61            fieldNames.add(fieldName);
 62            idx++;
 63        }
 64    }
 65
 66    public void append(final HCatFieldSchema hfs) throws HCatException{
 67      if(hfs == null)
 68        throw new HCatException("Attempt to append null HCatFieldSchema in HCatSchema.");
 69
 70      String fieldName = hfs.getName();
 71      if(fieldPositionMap.containsKey(fieldName))
 72        throw new HCatException("Attempt to append HCatFieldSchema with already " +
 73            "existing name: " + fieldName + ".");
 74
 75      this.fieldSchemas.add(hfs);
 76      this.fieldNames.add(fieldName);
 77      this.fieldPositionMap.put(fieldName, this.size()-1);
 78    }
 79
 80    /**
 81     *  Users are not allowed to modify the list directly, since HCatSchema
 82     *  maintains internal state. Use append/remove to modify the schema.
 83     */
 84    public List<HCatFieldSchema> getFields(){
 85        return Collections.unmodifiableList(this.fieldSchemas);
 86    }
 87
 88    /**
 89     * @param fieldName
 90     * @return the index of field named fieldName in Schema. If field is not
 91     * present, returns null.
 92     */
 93    public Integer getPosition(String fieldName) {
 94      return fieldPositionMap.get(fieldName);
 95    }
 96
 97    public HCatFieldSchema get(String fieldName) throws HCatException {
 98        return get(getPosition(fieldName));
 99    }
100
101    public List<String> getFieldNames(){
102        return this.fieldNames;
103    }
104
105    public HCatFieldSchema get(int position) {
106        return fieldSchemas.get(position);
107    }
108
109    public int size(){
110      return fieldSchemas.size();
111    }
112
113    public void remove(final HCatFieldSchema hcatFieldSchema) throws HCatException {
114
115      if(!fieldSchemas.contains(hcatFieldSchema)){
116        throw new HCatException("Attempt to delete a non-existent column from HCat Schema: "+ hcatFieldSchema);
117      }
118
119      fieldSchemas.remove(hcatFieldSchema);
120      fieldPositionMap.remove(hcatFieldSchema);
121      fieldNames.remove(hcatFieldSchema.getName());
122    }
123
124    @Override
125    public String toString() {
126        boolean first = true;
127        StringBuilder sb = new StringBuilder();
128        for (HCatFieldSchema hfs : fieldSchemas){
129            if (!first){
130                sb.append(",");
131            }else{
132                first = false;
133            }
134            if (hfs.getName() != null){
135                sb.append(hfs.getName());
136                sb.append(":");
137            }
138            sb.append(hfs.toString());
139        }
140        return sb.toString();
141    }
142}