apex-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chinmaykolhatkar <...@git.apache.org>
Subject [GitHub] incubator-apex-malhar pull request: MLHR-1961 enhanced existing cs...
Date Mon, 04 Jan 2016 05:20:54 GMT
Github user chinmaykolhatkar commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/154#discussion_r48704882
  
    --- Diff: contrib/src/main/java/com/datatorrent/contrib/parser/CellProcessorBuilder.java
---
    @@ -0,0 +1,456 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF 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
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License 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.datatorrent.contrib.parser;
    +
    +import java.util.Map;
    +
    +import org.supercsv.cellprocessor.Optional;
    +import org.supercsv.cellprocessor.ParseBool;
    +import org.supercsv.cellprocessor.ParseChar;
    +import org.supercsv.cellprocessor.ParseDate;
    +import org.supercsv.cellprocessor.ParseDouble;
    +import org.supercsv.cellprocessor.ParseInt;
    +import org.supercsv.cellprocessor.ParseLong;
    +import org.supercsv.cellprocessor.constraint.DMinMax;
    +import org.supercsv.cellprocessor.constraint.Equals;
    +import org.supercsv.cellprocessor.constraint.LMinMax;
    +import org.supercsv.cellprocessor.constraint.StrMinMax;
    +import org.supercsv.cellprocessor.constraint.StrRegEx;
    +import org.supercsv.cellprocessor.constraint.Strlen;
    +import org.supercsv.cellprocessor.ift.CellProcessor;
    +import org.supercsv.cellprocessor.ift.DoubleCellProcessor;
    +import org.supercsv.cellprocessor.ift.LongCellProcessor;
    +import org.supercsv.util.CsvContext;
    +
    +import org.apache.commons.lang3.StringUtils;
    +
    +import com.datatorrent.contrib.parser.DelimitedSchema.FIELD_TYPE;
    +
    +/**
    + * Helper class with methods to generate CellProcessor objects. Cell processors
    + * are an integral part of reading and writing with Super CSV - they automate
    + * the data type conversions, and enforce constraints. They implement the chain
    + * of responsibility design pattern - each processor has a single, well-defined
    + * purpose and can be chained together with other processors to fully automate
    + * all of the required conversions and constraint validation for a single
    + * delimited record.
    + * 
    + */
    +public class CellProcessorBuilder
    +{
    +
    +  /**
    +   * Method to get cell processors for given field type and constraints
    +   * 
    +   * @param fieldType
    +   *          data type of the field
    +   * @param constraints
    +   *          a map of constraints
    +   * @return
    +   */
    +  public static CellProcessor getCellProcessor(FIELD_TYPE fieldType, Map<String, Object>
constraints)
    +  {
    +    switch (fieldType) {
    +      case STRING:
    +        return getStringCellProcessor(constraints);
    +      case INTEGER:
    +        return getIntegerCellProcessor(constraints);
    +      case LONG:
    +        return getLongCellProcessor(constraints);
    +      case FLOAT:
    +      case DOUBLE:
    +        return getDoubleCellProcessor(constraints);
    +      case CHARACTER:
    +        return getCharCellProcessor(constraints);
    +      case BOOLEAN:
    +        return getBooleanCellProcessor(constraints);
    +      case DATE:
    +        return getDateCellProcessor(constraints);
    +      default:
    +        return null;
    +    }
    +  }
    +
    +  /**
    +   * Method to get cellprocessor for String with constraints. These constraints
    +   * are evaluated against the String field for which this cellprocessor is
    +   * defined.
    +   * 
    +   * @param constraints
    +   *          map of constraints applicable to String
    +   * @return CellProcessor
    +   */
    +  private static CellProcessor getStringCellProcessor(Map<String, Object> constraints)
    +  {
    +    Boolean required = constraints.get(DelimitedSchema.REQUIRED) == null ? null : Boolean
    +        .parseBoolean((String)constraints.get(DelimitedSchema.REQUIRED));
    +    Integer strLen = constraints.get(DelimitedSchema.LENGTH) == null ? null : Integer.parseInt((String)constraints
    +        .get(DelimitedSchema.LENGTH));
    +    Integer minLength = constraints.get(DelimitedSchema.MIN_LENGTH) == null ? null :
Integer
    +        .parseInt((String)constraints.get(DelimitedSchema.MIN_LENGTH));
    +    Integer maxLength = constraints.get(DelimitedSchema.MAX_LENGTH) == null ? null :
Integer
    +        .parseInt((String)constraints.get(DelimitedSchema.MAX_LENGTH));
    +    String equals = constraints.get(DelimitedSchema.EQUALS) == null ? null : (String)constraints
    +        .get(DelimitedSchema.EQUALS);
    +    String pattern = constraints.get(DelimitedSchema.REGEX_PATTERN) == null ? null :
(String)constraints
    +        .get(DelimitedSchema.REGEX_PATTERN);
    +
    +    CellProcessor cellProcessor = null;
    +    if (StringUtils.isNotBlank(equals)) {
    --- End diff --
    
    For the if/else block:
    Does this mean there is its not allowed to have multiple of them as constraints?
    If yes, why?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message