001/*
002 * (C) Copyright 2018 Nuxeo (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     pierre
018 */
019package org.nuxeo.runtime.avro;
020
021import java.util.Objects;
022
023import org.nuxeo.common.xmap.annotation.XNode;
024import org.nuxeo.common.xmap.annotation.XObject;
025
026/**
027 * The Avro forbidden character replacement descriptor.<br>
028 * Avro allows alphanumeric characters and underscores in names.<br>
029 * Nuxeo Studio allows alphanumeric characters, underscores and dashes that have to be replaced by another symbol.<br>
030 * <br>
031 * The default contributions provide replacement for :<br>
032 * - "-" as "__dash__"<br>
033 * - ":" as "__colon__<br>
034 * - ";" as "__semicolon__"<br>
035 * - and with higher priority "__" as "____" to ensure no user string is wrongly replaced.<br>
036 *
037 * @since 10.2
038 */
039@XObject("replacement")
040public class AvroReplacementDescriptor implements Comparable<AvroReplacementDescriptor> {
041
042    @XNode("@forbidden")
043    protected String forbidden;
044
045    @XNode("@replacement")
046    protected String replacement;
047
048    @XNode("@priority")
049    protected int priority;
050
051    @SuppressWarnings("NullableProblems")
052    @Override
053    public int compareTo(AvroReplacementDescriptor o) {
054        if (priority != o.priority) {
055            return Integer.compare(priority, o.priority);
056        } else {
057            return Objects.compare(forbidden, o.forbidden, String::compareTo);
058        }
059    }
060
061    @Override
062    public boolean equals(Object obj) {
063        if (this == obj) {
064            return true;
065        }
066        if (obj == null) {
067            return false;
068        }
069        if (getClass() != obj.getClass()) {
070            return false;
071        }
072        AvroReplacementDescriptor other = (AvroReplacementDescriptor) obj;
073        return Objects.equals(forbidden, other.forbidden);
074    }
075
076    public String getForbidden() {
077        return forbidden;
078    }
079
080    public String getReplacement() {
081        return replacement;
082    }
083
084    @Override
085    public int hashCode() {
086        final int prime = 31;
087        int result = 1;
088        result = prime * result + (forbidden == null ? 0 : forbidden.hashCode());
089        return result;
090    }
091
092    @Override
093    public String toString() {
094        return "AvroReplacementDescriptor [forbidden=" + forbidden + ", " + "replacement=" + replacement + ","
095                + " priority=" + priority + "]";
096    }
097
098}