1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.jcabi.jdbc;
31
32 import java.math.BigDecimal;
33 import java.util.AbstractMap;
34 import java.util.Date;
35 import java.util.Map;
36 import java.util.UUID;
37 import java.util.stream.Collectors;
38 import java.util.stream.Stream;
39 import lombok.AccessLevel;
40 import lombok.RequiredArgsConstructor;
41
42
43
44
45
46
47 @RequiredArgsConstructor(access = AccessLevel.PRIVATE)
48 final class DefaultMappings implements Outcome.Mappings {
49
50
51
52 private final Map<Class<?>, Outcome.Mapping<?>> map;
53
54
55
56
57 DefaultMappings() {
58 this(1);
59 }
60
61
62
63
64
65
66 DefaultMappings(final int column) {
67 this(
68 new AbstractMap.SimpleImmutableEntry<>(
69 String.class, rs -> rs.getString(column)
70 ),
71 new AbstractMap.SimpleImmutableEntry<>(
72 Long.class, rs -> rs.getLong(column)
73 ),
74 new AbstractMap.SimpleImmutableEntry<>(
75 Boolean.class, rs -> rs.getBoolean(column)
76 ),
77 new AbstractMap.SimpleImmutableEntry<>(
78 Byte.class, rs -> rs.getByte(column)
79 ),
80 new AbstractMap.SimpleImmutableEntry<>(
81 Date.class, rs -> rs.getDate(column)
82 ),
83 new AbstractMap.SimpleImmutableEntry<>(
84 Utc.class, rs -> new Utc(Utc.getTimestamp(rs, column))
85 ),
86 new AbstractMap.SimpleImmutableEntry<>(
87 byte[].class, rs -> rs.getBytes(column)
88 ),
89 new AbstractMap.SimpleImmutableEntry<>(
90 BigDecimal.class, rs -> rs.getBigDecimal(column)
91 ),
92 new AbstractMap.SimpleImmutableEntry<>(
93 UUID.class, rs -> rs.getObject(column, UUID.class)
94 )
95 );
96 }
97
98
99
100
101
102
103 @SafeVarargs
104 private DefaultMappings(
105 final Map.Entry<Class<?>, Outcome.Mapping<?>>... mappings
106 ) {
107 this(
108 Stream
109 .of(mappings)
110 .collect(
111 Collectors.toMap(
112 Map.Entry::getKey,
113 Map.Entry::getValue
114 )
115 )
116 );
117 }
118
119 @Override
120 @SuppressWarnings("unchecked")
121 public <X> Outcome.Mapping<X> forType(final Class<? extends X> tpe) {
122 if (!this.map.containsKey(tpe)) {
123 throw new IllegalArgumentException(
124 String.format("Type %s is not supported", tpe.getName())
125 );
126 }
127 return (Outcome.Mapping<X>) this.map.get(tpe);
128 }
129 }