You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GenericRecordUtil.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.util;
  20. import java.util.Arrays;
  21. import java.util.Collections;
  22. import java.util.LinkedHashMap;
  23. import java.util.Map;
  24. import java.util.function.Supplier;
  25. import java.util.stream.Collectors;
  26. @Internal
  27. public final class GenericRecordUtil {
  28. private GenericRecordUtil() {}
  29. public static Map<String, Supplier<?>>
  30. getGenericProperties(String val1, Supplier<?> sup1) {
  31. return Collections.singletonMap(val1, sup1);
  32. }
  33. public static Map<String, Supplier<?>> getGenericProperties(
  34. String val1, Supplier<?> sup1,
  35. String val2, Supplier<?> sup2
  36. ) {
  37. return getGenericProperties(val1, sup1, val2, sup2, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
  38. }
  39. public static Map<String, Supplier<?>> getGenericProperties(
  40. String val1, Supplier<?> sup1,
  41. String val2, Supplier<?> sup2,
  42. String val3, Supplier<?> sup3
  43. ) {
  44. return getGenericProperties(val1, sup1, val2, sup2, val3, sup3, null, null, null, null, null, null, null, null, null, null, null, null);
  45. }
  46. public static Map<String, Supplier<?>> getGenericProperties(
  47. String val1, Supplier<?> sup1,
  48. String val2, Supplier<?> sup2,
  49. String val3, Supplier<?> sup3,
  50. String val4, Supplier<?> sup4
  51. ) {
  52. return getGenericProperties(val1, sup1, val2, sup2, val3, sup3, val4, sup4, null, null, null, null, null, null, null, null, null, null);
  53. }
  54. public static Map<String, Supplier<?>> getGenericProperties(
  55. String val1, Supplier<?> sup1,
  56. String val2, Supplier<?> sup2,
  57. String val3, Supplier<?> sup3,
  58. String val4, Supplier<?> sup4,
  59. String val5, Supplier<?> sup5
  60. ) {
  61. return getGenericProperties(val1, sup1, val2, sup2, val3, sup3, val4, sup4, val5, sup5, null, null, null, null, null, null, null, null);
  62. }
  63. public static Map<String, Supplier<?>> getGenericProperties(
  64. String val1, Supplier<?> sup1,
  65. String val2, Supplier<?> sup2,
  66. String val3, Supplier<?> sup3,
  67. String val4, Supplier<?> sup4,
  68. String val5, Supplier<?> sup5,
  69. String val6, Supplier<?> sup6
  70. ) {
  71. return getGenericProperties(val1, sup1, val2, sup2, val3, sup3, val4, sup4, val5, sup5, val6, sup6, null, null, null, null, null, null);
  72. }
  73. public static Map<String, Supplier<?>> getGenericProperties(
  74. String val1, Supplier<?> sup1,
  75. String val2, Supplier<?> sup2,
  76. String val3, Supplier<?> sup3,
  77. String val4, Supplier<?> sup4,
  78. String val5, Supplier<?> sup5,
  79. String val6, Supplier<?> sup6,
  80. String val7, Supplier<?> sup7
  81. ) {
  82. return getGenericProperties(val1, sup1, val2, sup2, val3, sup3, val4, sup4, val5, sup5, val6, sup6, val7, sup7, null, null, null, null);
  83. }
  84. public static Map<String, Supplier<?>> getGenericProperties(
  85. String val1, Supplier<?> sup1,
  86. String val2, Supplier<?> sup2,
  87. String val3, Supplier<?> sup3,
  88. String val4, Supplier<?> sup4,
  89. String val5, Supplier<?> sup5,
  90. String val6, Supplier<?> sup6,
  91. String val7, Supplier<?> sup7,
  92. String val8, Supplier<?> sup8
  93. ) {
  94. return getGenericProperties(val1, sup1, val2, sup2, val3, sup3, val4, sup4, val5, sup5, val6, sup6, val7, sup7, val8, sup8, null, null);
  95. }
  96. public static Map<String, Supplier<?>> getGenericProperties(
  97. String val1, Supplier<?> sup1,
  98. String val2, Supplier<?> sup2,
  99. String val3, Supplier<?> sup3,
  100. String val4, Supplier<?> sup4,
  101. String val5, Supplier<?> sup5,
  102. String val6, Supplier<?> sup6,
  103. String val7, Supplier<?> sup7,
  104. String val8, Supplier<?> sup8,
  105. String val9, Supplier<?> sup9
  106. ) {
  107. final Map<String,Supplier<?>> m = new LinkedHashMap<>();
  108. final String[] vals = { val1, val2, val3, val4, val5, val6, val7, val8, val9 };
  109. final Supplier<?>[] sups = { sup1, sup2, sup3, sup4, sup5, sup6, sup7, sup8, sup9 };
  110. for (int i=0; i<vals.length && vals[i] != null; i++) {
  111. assert(sups[i] != null);
  112. if ("base".equals(vals[i])) {
  113. Object baseMap = sups[i].get();
  114. assert(baseMap instanceof Map);
  115. //noinspection unchecked
  116. m.putAll((Map<String,Supplier<?>>)baseMap);
  117. } else {
  118. m.put(vals[i], sups[i]);
  119. }
  120. }
  121. return Collections.unmodifiableMap(m);
  122. }
  123. public static <T extends Enum<?>> Supplier<T> safeEnum(T[] values, Supplier<Number> ordinal) {
  124. return safeEnum(values, ordinal, null);
  125. }
  126. public static <T extends Enum<?>> Supplier<T> safeEnum(T[] values, Supplier<Number> ordinal, T defaultVal) {
  127. int ord = ordinal.get().intValue();
  128. return () -> (0 <= ord && ord < values.length) ? values[ord] : defaultVal;
  129. }
  130. public static Supplier<AnnotatedFlag> getBitsAsString(Supplier<Number> flags, final BitField[] masks, final String[] names) {
  131. int[] iMasks = Arrays.stream(masks).mapToInt(BitField::getMask).toArray();
  132. return () -> new AnnotatedFlag(flags, iMasks, names, false);
  133. }
  134. public static Supplier<AnnotatedFlag> getBitsAsString(Supplier<Number> flags, final int[] masks, final String[] names) {
  135. return () -> new AnnotatedFlag(flags, masks, names, false);
  136. }
  137. public static Supplier<AnnotatedFlag> getEnumBitsAsString(Supplier<Number> flags, final int[] masks, final String[] names) {
  138. return () -> new AnnotatedFlag(flags, masks, names, true);
  139. }
  140. public static class AnnotatedFlag {
  141. private final Supplier<Number> value;
  142. private final Map<Integer,String> masks = new LinkedHashMap<>();
  143. private final boolean exactMatch;
  144. AnnotatedFlag(Supplier<Number> value, int[] masks, String[] names, boolean exactMatch) {
  145. assert(masks.length == names.length);
  146. this.value = value;
  147. this.exactMatch = exactMatch;
  148. for (int i=0; i<masks.length; i++) {
  149. this.masks.put(masks[i], names[i]);
  150. }
  151. }
  152. public Supplier<Number> getValue() {
  153. return value;
  154. }
  155. public String getDescription() {
  156. final int val = value.get().intValue();
  157. return masks.entrySet().stream().
  158. filter(e -> match(val, e.getKey())).
  159. map(Map.Entry::getValue).
  160. collect(Collectors.joining(" | "));
  161. }
  162. private boolean match(final int val, int mask) {
  163. return exactMatch ? (val == mask) : ((val & mask) == mask);
  164. }
  165. }
  166. }