Skip to content

Commit 6b0d8e4

Browse files
committed
converter
1 parent 8852a02 commit 6b0d8e4

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.github.housepower.jdbc.convert;
16+
17+
import com.github.housepower.jdbc.misc.BytesCharSeq;
18+
19+
20+
public class ByteArrayToBytesCharSeqConverter implements Converter<byte[], BytesCharSeq> {
21+
22+
@Override
23+
public BytesCharSeq convert(byte[] source) {
24+
return new BytesCharSeq(source);
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.github.housepower.jdbc.convert;
16+
17+
import com.github.housepower.jdbc.misc.BytesCharSeq;
18+
19+
20+
public class BytesCharSeqToByteArrayConverter implements Converter<BytesCharSeq, byte[]> {
21+
22+
@Override
23+
public byte[] convert(BytesCharSeq source) {
24+
return source.bytes();
25+
}
26+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.github.housepower.jdbc.convert;
16+
17+
import java.lang.reflect.ParameterizedType;
18+
19+
/**
20+
* A converter converts a source object of type {@code S} to a target of type {@code T}.
21+
*
22+
* <p>Implementations of this interface are thread-safe and can be shared.
23+
*
24+
* @param <S> the source type
25+
* @param <T> the target type
26+
*/
27+
@FunctionalInterface
28+
public interface Converter<S, T> {
29+
30+
/**
31+
* Convert the source object of type {@code S} to target type {@code T}.
32+
*
33+
* @param source the source object to convert, which must be an instance of {@code S}
34+
* @return the converted object, which must be an instance of {@code T}
35+
* @throws IllegalArgumentException if the source cannot be converted to the desired target type
36+
*/
37+
T convert(S source);
38+
39+
@SuppressWarnings("unchecked")
40+
default Class<T> sourceType() {
41+
return (Class<T>) ((ParameterizedType) this.getClass().getGenericInterfaces()[0]).getActualTypeArguments()[0];
42+
}
43+
44+
@SuppressWarnings("unchecked")
45+
default Class<T> targetType() {
46+
return (Class<T>) ((ParameterizedType) this.getClass().getGenericInterfaces()[0]).getActualTypeArguments()[1];
47+
}
48+
}

0 commit comments

Comments
 (0)