Skip to content

Commit

Permalink
converter
Browse files Browse the repository at this point in the history
  • Loading branch information
pan3793 committed Dec 27, 2020
1 parent 21d8c48 commit d297e26
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.housepower.jdbc.convert;

import com.github.housepower.jdbc.misc.BytesCharSeq;


public class ByteArrayToBytesCharSeqConverter implements Converter<byte[], BytesCharSeq> {

@Override
public BytesCharSeq convert(byte[] source) {
return new BytesCharSeq(source);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.housepower.jdbc.convert;

import com.github.housepower.jdbc.misc.BytesCharSeq;


public class BytesCharSeqToByteArrayConverter implements Converter<BytesCharSeq, byte[]> {

@Override
public byte[] convert(BytesCharSeq source) {
return source.bytes();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.housepower.jdbc.convert;

import java.lang.reflect.ParameterizedType;

/**
* A converter converts a source object of type {@code S} to a target of type {@code T}.
*
* <p>Implementations of this interface are thread-safe and can be shared.
*
* @param <S> the source type
* @param <T> the target type
*/
@FunctionalInterface
public interface Converter<S, T> {

/**
* Convert the source object of type {@code S} to target type {@code T}.
*
* @param source the source object to convert, which must be an instance of {@code S}
* @return the converted object, which must be an instance of {@code T}
* @throws IllegalArgumentException if the source cannot be converted to the desired target type
*/
T convert(S source);

@SuppressWarnings("unchecked")
default Class<T> sourceType() {
return (Class<T>) ((ParameterizedType) this.getClass().getGenericInterfaces()[0]).getActualTypeArguments()[0];
}

@SuppressWarnings("unchecked")
default Class<T> targetType() {
return (Class<T>) ((ParameterizedType) this.getClass().getGenericInterfaces()[0]).getActualTypeArguments()[1];
}
}

0 comments on commit d297e26

Please sign in to comment.