content: /*
   * 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.aliyun.odps;
  
  import java.util.ArrayList;
  
  import org.codehaus.jackson.JsonNode;
  
  import com.google.gson.JsonArray;
  import com.google.gson.JsonObject;
  
  /**
   * Created by yinyue on 15-3-17.
   */
  public final class Shard {
  
    @Deprecated
    private long hubLifecycle;
  
    private long shardNum;
    private ArrayList<String> distributeCols;
    private ArrayList<String> sortCols;
  
    Shard() {
      hubLifecycle = -1;
    }
  
    public static Shard parseShard(JsonObject tree) {
      try {
        Shard shard = new Shard();
  
        if (tree.has("ShardNum")) {
          shard.setShardNum(tree.get("ShardNum").getAsLong());
        }
  
        if (tree.has("HubLifecycle")) {
          shard.setHubLifecycle(tree.get("HubLifecycle").getAsLong());
        }
  
        if (tree.has("DistributeCols")) {
          JsonArray distributeCols = tree.get("DistributeCols").getAsJsonArray();
          ArrayList<String> shardDistributeCols = new ArrayList<String>();
          for (int i = 0; i < distributeCols.size(); ++i) {
            String col = distributeCols.get(i).getAsString();
            shardDistributeCols.add(col);
          }
          shard.setDistributeColumnNames(shardDistributeCols);
        }
  
        if (tree.has("SortCols")) {
          JsonArray sortCols = tree.get("SortCols").getAsJsonArray();
          ArrayList<String> shardSortCols = new ArrayList<String>();
          for (int i = 0; i < sortCols.size(); ++i) {
            String col = sortCols.get(i).getAsString();
            shardSortCols.add(col);
          }
          shard.setSortColumnNames(shardSortCols);
        }
  
        return shard;
  
      } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
      }
    }
    
    /**
     * @deprecated use another method parseShard(JSONObject tree)
     */
    public static Shard parseShard(JsonNode tree) {
      try {
        Shard shard = new Shard();
  
        JsonNode node = tree.get("ShardNum");
        if (node != null && !node.isNull()) {
          shard.setShardNum(node.asLong());
        }
  
        node = tree.get("HubLifecycle");
        if (node != null && !node.isNull()) {
          shard.setHubLifecycle(node.asLong());
        }
  
        JsonNode distributeCols = tree.get("DistributeCols");
        if (distributeCols != null && !distributeCols.isNull() && distributeCols.isArray()) {
          ArrayList<String> shardDistributeCols = new ArrayList<String>();
          for (JsonNode col : distributeCols) {
            shardDistributeCols.add(col.asText());
          }
          shard.setDistributeColumnNames(shardDistributeCols);
        }
  
        JsonNode sortCols = tree.get("SortCols");
        if (sortCols != null && !sortCols.isNull() && sortCols.isArray()) {
          ArrayList<String> shardSortCols = new ArrayList<String>();
          for (JsonNode col : sortCols) {
            shardSortCols.add(col.asText());
          }
          shard.setSortColumnNames(shardSortCols);
        }
  
        return shard;
  
      } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
      }
    }
  
    /**
     * 返回表的HubLifecycle 单位：天
     *
     * @return lifecycle， 如果表不为hub表返回 -1
     */
    @Deprecated
    public long getHubLifecycle() {
      return hubLifecycle;
    }
  
    /**
     * 返回shard数量
     *
     * @return shard数
     */
    public long getShardNum() {
      return shardNum;
    }
  
    /**
     * 返回创建shard表时指定的cluster by列名
     *
     * @return 列名数组, 不存在则返回空数组
     */
    public ArrayList<String> getDistributeColumnNames() {
      return distributeCols;
    }
  
    /**
     * 返回创建shard表时指定的sorted by列名
     *
     * @return 列名数组，不存在则返回空数组
     */
    public ArrayList<String> getSortColumnNames() {
      return sortCols;
    }
  
    @Deprecated
    private void setHubLifecycle(long lifecycle) {
      hubLifecycle = lifecycle;
    }
  
    private void setShardNum(long num) {
      shardNum = num;
    }
  
    private void setDistributeColumnNames(ArrayList<String> columnNames) {
      distributeCols = columnNames;
    }
  
    private void setSortColumnNames(ArrayList<String> columnNames) {
      sortCols = columnNames;
    }
  }
  