001/**
002 * Copyright (C) 2006-2026 Talend Inc. - www.talend.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.server.front.model;
017
018import java.beans.ConstructorProperties;
019import java.util.LinkedHashMap;
020import java.util.Map;
021
022import lombok.Data;
023
024@Data
025public final class Entry {
026
027    private final String name;
028
029    private final String rawName;
030
031    private final Schema.Type type;
032
033    private final boolean nullable;
034
035    private final boolean metadata;
036
037    private final boolean errorCapable;
038
039    private final boolean valid;
040
041    private final Schema elementSchema;
042
043    private final String comment;
044
045    private final Map<String, String> props = new LinkedHashMap<>(0);
046
047    private final Object defaultValue;
048
049    @ConstructorProperties({ "name", "rawName", "type", "nullable", "metadata", "errorCapable",
050            "valid", "elementSchema", "comment", "props", "defaultValue" })
051    // Checkstyle off to let have 11 parameters to this constructor (normally 10 max)
052    // CHECKSTYLE:OFF
053    public Entry(
054            final String name,
055            final String rawName,
056            final Schema.Type type,
057            final boolean nullable,
058            final boolean metadata,
059            final boolean errorCapable,
060            final boolean valid,
061            final Schema elementSchema,
062            final String comment,
063            final Map<String, String> props,
064            final Object defaultValue) {
065        // CHECKSTYLE:ON
066        this.name = name;
067        this.rawName = rawName;
068        this.type = type;
069        this.nullable = nullable;
070        this.metadata = metadata;
071        this.errorCapable = errorCapable;
072        this.valid = valid;
073        this.elementSchema = elementSchema;
074        this.comment = comment;
075        if (props != null) {
076            this.props.putAll(props);
077        }
078        this.defaultValue = defaultValue;
079    }
080
081    private Object getInternalDefaultValue() {
082        return defaultValue;
083    }
084
085    @SuppressWarnings("unchecked")
086    public <T> T getDefaultValue() {
087        if (defaultValue == null) {
088            return null;
089        }
090
091        return switch (this.getType()) {
092            case INT -> (T) ((Integer) ((Number) this.getInternalDefaultValue()).intValue());
093            case LONG -> (T) ((Long) ((Number) this.getInternalDefaultValue()).longValue());
094            case FLOAT -> (T) ((Float) ((Number) this.getInternalDefaultValue()).floatValue());
095            case DOUBLE -> (T) ((Double) ((Number) this.getInternalDefaultValue()).doubleValue());
096            default -> (T) this.getInternalDefaultValue();
097        };
098
099    }
100
101    /**
102     * * Returns the property value associated with the given key.
103     * * @param key the property key
104     * * @return the property value, or {@code null} if there is no mapping for the given key
105     */
106    public String getOriginalFieldName() {
107        return rawName != null ? rawName : name;
108    }
109
110    public String getProp(final String key) {
111        return this.props.get(key);
112    }
113
114}