001package de.isas.mztab2.model;
002
003import de.isas.mztab2.model.IndexedElement;
004import java.util.Objects;
005
006/*
007 * Copyright 2020 Leibniz-Institut für Analytische Wissenschaften – ISAS – e.V..
008 *
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 */
021/**
022 * Implementation of IndexedElement where more information is required than just
023 * the id. Wraps the actual object and exposes it via {@link #getPayload()}.
024 *
025 * @author nilshoffmann
026 */
027public class IndexedElementImpl implements IndexedElement {
028
029    private final Integer id;
030    private final String elementType;
031    private final Object payload;
032
033    /**
034     * Create a new indexed element implementation for the provided id, element
035     * type and payload.
036     *
037     * @param id the id.
038     * @param elementType the element type string.
039     * @param payload the payload, any indexed domain object.
040     */
041    public IndexedElementImpl(Integer id, String elementType, Object payload) {
042        this.id = id;
043        this.elementType = elementType;
044        this.payload = payload;
045    }
046
047    @Override
048    public Integer getId() {
049        return id;
050    }
051
052    /**
053     * Returns the element type string.
054     * This is used by MetadataElement in the <pre>jmztabm-io</pre> module.
055     *
056     * @return the element type string.
057     */
058    public String getElementType() {
059        return elementType;
060    }
061
062    /**
063     * Returns the wrapped object.
064     *
065     * @return the payload.
066     */
067    public Object getPayload() {
068        return payload;
069    }
070
071    @Override
072    public boolean equals(java.lang.Object o) {
073        if (this == o) {
074            return true;
075        }
076        if (o == null || getClass() != o.getClass()) {
077            return false;
078        }
079        IndexedElement other = (IndexedElement) o;
080        return Objects.equals(this.id, other.getId());
081    }
082
083    @Override
084    public int hashCode() {
085        return Objects.hash(id);
086    }
087
088    @Override
089    public String toString() {
090        StringBuilder sb = new StringBuilder();
091        sb.append("class IndexedElement {\n");
092
093        sb.append("    id: ").append(toIndentedString(id)).append("\n");
094        sb.append("}");
095        return sb.toString();
096    }
097
098    /**
099     * Convert the given object to string with each line indented by 4 spaces
100     * (except the first line).
101     */
102    private String toIndentedString(java.lang.Object o) {
103        if (o == null) {
104            return "null";
105        }
106        return o.toString().replace("\n", "\n    ");
107    }
108}