1 /*
2 * Copyright (c) 2012-2023, jcabi.com
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met: 1) Redistributions of source code must retain the above
8 * copyright notice, this list of conditions and the following
9 * disclaimer. 2) Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution. 3) Neither the name of the jcabi.com nor
13 * the names of its contributors may be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
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 * Default mappings for types.
44 *
45 * @since 0.17.6
46 */
47 @RequiredArgsConstructor(access = AccessLevel.PRIVATE)
48 final class DefaultMappings implements Outcome.Mappings {
49 /**
50 * Per-type extraction methods.
51 */
52 private final Map<Class<?>, Outcome.Mapping<?>> map;
53
54 /**
55 * Ctor.
56 */
57 DefaultMappings() {
58 this(1);
59 }
60
61 /**
62 * Ctor.
63 *
64 * @param column Column position.
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 * Ctor.
100 *
101 * @param mappings Mappings.
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 }