View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.infra.merge.result.impl.local;
19  
20  import com.google.common.base.Preconditions;
21  import org.apache.shardingsphere.infra.util.json.JsonUtils;
22  
23  import java.time.LocalDateTime;
24  import java.util.LinkedHashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Optional;
28  import java.util.Properties;
29  import java.util.stream.Collectors;
30  import java.util.stream.Stream;
31  
32  /**
33   * Local data query result row.
34   */
35  public final class LocalDataQueryResultRow {
36      
37      private final List<Object> data;
38      
39      public LocalDataQueryResultRow(final Object... data) {
40          this.data = Stream.of(data).map(this::convert).collect(Collectors.toList());
41      }
42      
43      private Object convert(final Object data) {
44          if (null == data) {
45              return "";
46          }
47          if (data instanceof Optional) {
48              return ((Optional<?>) data).isPresent() ? convert(((Optional<?>) data).get()) : "";
49          }
50          if (data instanceof String) {
51              return data;
52          }
53          if (data instanceof Boolean || data instanceof Integer || data instanceof Long || data instanceof LocalDateTime) {
54              return data.toString();
55          }
56          if (data instanceof Enum) {
57              return ((Enum<?>) data).name();
58          }
59          if (data instanceof Properties) {
60              return ((Properties) data).isEmpty() ? "" : JsonUtils.toJsonString(convert((Properties) data));
61          }
62          if (data instanceof Map) {
63              return ((Map<?, ?>) data).isEmpty() ? "" : JsonUtils.toJsonString(data);
64          }
65          return JsonUtils.toJsonString(data);
66      }
67      
68      private Map<Object, Object> convert(final Properties props) {
69          return props.keySet().stream().map(Object::toString).sorted().collect(Collectors.toMap(each -> each, props::get, (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
70      }
71      
72      /**
73       * Get data from cell.
74       * 
75       * @param columnIndex column index
76       * @return data from cell
77       */
78      public Object getCell(final int columnIndex) {
79          Preconditions.checkArgument(columnIndex > 0 && columnIndex < data.size() + 1);
80          return data.get(columnIndex - 1);
81      }
82  }