|
| 1 | +/** |
| 2 | + * Copyright 2016-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.mybatis.dynamic.sql.select.function; |
| 17 | + |
| 18 | +import java.util.Objects; |
| 19 | +import java.util.Optional; |
| 20 | + |
| 21 | +import org.mybatis.dynamic.sql.BindableColumn; |
| 22 | + |
| 23 | +/** |
| 24 | + * Represents a function that can change the underlying type. For example, converting a binary field for a base64 |
| 25 | + * string, or an integer to a string, etc. |
| 26 | + * |
| 27 | + * <p>Thanks to @endink for the idea. |
| 28 | + * |
| 29 | + * @author Jeff Butler |
| 30 | + * |
| 31 | + * @param <T> The type of the underlying column. For example, if a function converts a VARCHR to an INT, then the |
| 32 | + * underlying type will be a String |
| 33 | + * @param <R> The type of the column after the conversion. For example, if a function converts a VARCHR to an INT, then |
| 34 | + * the converted type will be Integer |
| 35 | + * @param <U> the specific subtype that implements the function |
| 36 | + */ |
| 37 | +public abstract class AbstractTypeConvertingFunction<T, R, U extends AbstractTypeConvertingFunction<T, R, U>> |
| 38 | + implements BindableColumn<R> { |
| 39 | + protected BindableColumn<T> column; |
| 40 | + protected String alias; |
| 41 | + |
| 42 | + protected AbstractTypeConvertingFunction(BindableColumn<T> column) { |
| 43 | + this.column = Objects.requireNonNull(column); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public Optional<String> alias() { |
| 48 | + return Optional.ofNullable(alias); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public U as(String alias) { |
| 53 | + U newThing = copy(); |
| 54 | + newThing.alias = alias; |
| 55 | + return newThing; |
| 56 | + } |
| 57 | + |
| 58 | + protected abstract U copy(); |
| 59 | +} |
0 commit comments