/**
 * Copyright (c) 2013, Cloudera, Inc. All Rights Reserved.
 *
 * Cloudera, Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"). You may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for
 * the specific language governing permissions and limitations under the
 * License.
 */
package com.cloudera.science.ml.core.records;

import java.util.List;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

/**
 * Utility functions for working with header files and {@code Spec} data.
 */
public final class Specs {

  public static boolean isNumeric(Spec spec, String fieldId) {
    FieldSpec fs = spec.getField(getFieldId(spec, fieldId));
    return fs.spec().getDataType().isNumeric();
  }
  
  public static Integer getFieldId(Spec spec, String value) {
    List<Integer> fieldIds = getFieldIds(spec, ImmutableList.of(value));
    if (fieldIds.isEmpty()) {
      throw new IllegalArgumentException("Could not find field " + value + " in spec");
    }
    return fieldIds.get(0);
  }
  
  public static List<Integer> getFieldIds(Spec spec, List<String> values) {
    if (values.isEmpty()) {
      return ImmutableList.of();
    }
    
    List<Integer> fieldIds;
    if (spec == null || spec.getField(values.get(0)) == null) {
      fieldIds = Lists.transform(values, new Function<String, Integer>() {
        @Override
        public Integer apply(String input) {
          try {
            return Integer.valueOf(input);
          } catch (NumberFormatException ignored) {
            throw new IllegalArgumentException("Did not recognize column ID: " + input);
          }
        }
      });
    } else {
      fieldIds = Lists.newArrayListWithExpectedSize(values.size());
      for (String value : values) {
        FieldSpec f = spec.getField(value);
        if (f != null) {
          fieldIds.add(f.position());
        }
      }
    }
    return fieldIds;
  }
  
  private Specs() {
  }
}

@@ -638,48 +638,8 @@
 t;%0A%0A
-import com.google.common.base.Function;%0A
 impo
@@ -1299,26 +1299,24 @@
 get(0);%0A  %7D%0A
-  
 %0A  public st
@@ -1451,13 +1451,8 @@
   %7D%0A
-    %0A
     
@@ -1469,24 +1469,76 @@
 er%3E fieldIds
+ = Lists.newArrayListWithExpectedSize(values.size())
 ;%0A    if (sp
@@ -1602,141 +1602,37 @@
    f
-ieldIds = Lists.transform(values, new Function%3CString, Integer%3E() %7B%0A        @Override%0A        public Integer apply(String input
+or (String value : values
 ) %7B%0A
-  
     
@@ -1651,25 +1651,29 @@
         
-  return 
+fieldIds.add(
 Integer.
@@ -1680,26 +1680,25 @@
 valueOf(
-input
+value)
 );%0A
-  
         
@@ -1732,21 +1732,13 @@
 ion 
-ignored
+e
 ) %7B%0A
-  
     
@@ -1816,18 +1816,16 @@
 %22 + 
-input
+value
 );%0A
-  
     
@@ -1840,102 +1840,22 @@
     
-  %7D%0A      %7D);%0A    %7D else %7B%0A      fieldIds = Lists.newArrayListWithExpectedSize(values.size());
+%7D%0A    %7D else %7B
 %0A   

Fix serialization bug in getFieldIds