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.Collection;
25  import java.util.LinkedHashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Optional;
29  import java.util.Properties;
30  import java.util.stream.Collectors;
31  import java.util.stream.Stream;
32  
33  /**
34   * Local data query result row.
35   */
36  public final class LocalDataQueryResultRow {
37      
38      private final List<Object> data;
39      
40      public LocalDataQueryResultRow(final Object... data) {
41          this.data = Stream.of(data).map(this::convert).collect(Collectors.toList());
42      }
43      
44      private Object convert(final Object data) {
45          if (null == data || "null".equals(data)) {
46              return "";
47          }
48          if (data instanceof Optional) {
49              return ((Optional<?>) data).isPresent() ? convert(((Optional<?>) data).get()) : "";
50          }
51          if (data instanceof String) {
52              return data;
53          }
54          if (data instanceof Boolean || data instanceof Integer || data instanceof Long || data instanceof LocalDateTime) {
55              return data.toString();
56          }
57          if (data instanceof Enum) {
58              return ((Enum<?>) data).name();
59          }
60          if (data instanceof Properties) {
61              return ((Properties) data).isEmpty() ? "" : JsonUtils.toJsonString(convert((Properties) data));
62          }
63          if (data instanceof Map) {
64              return ((Map<?, ?>) data).isEmpty() ? "" : JsonUtils.toJsonString(data);
65          }
66          if (data instanceof Collection) {
67              return ((Collection<?>) data).isEmpty() ? "" : JsonUtils.toJsonString(data);
68          }
69          return JsonUtils.toJsonString(data);
70      }
71      
72      private Map<Object, Object> convert(final Properties props) {
73          return props.keySet().stream().map(Object::toString).sorted().collect(Collectors.toMap(each -> each, props::get, (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
74      }
75      
76      /**
77       * Get data from cell.
78       *
79       * @param columnIndex column index
80       * @return data from cell
81       */
82      public Object getCell(final int columnIndex) {
83          Preconditions.checkArgument(columnIndex > 0 && columnIndex < data.size() + 1);
84          return data.get(columnIndex - 1);
85      }
86  }