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;
025import org.nuxeo.runtime.model.Descriptor;
026
027/**
028 * The Avro forbidden character replacement descriptor.<br>
029 * Avro allows alphanumeric characters and underscores in names.<br>
030 * Nuxeo Studio allows alphanumeric characters, underscores and dashes that have to be replaced by another symbol.<br>
031 * <br>
032 * The default contributions provide replacement for :<br>
033 * - "-" as "__dash__"<br>
034 * - ":" as "__colon__<br>
035 * - ";" as "__semicolon__"<br>
036 * - and with higher priority "__" as "____" to ensure no user string is wrongly replaced.<br>
037 *
038 * @since 10.2
039 */
040@XObject("replacement")
041public class AvroReplacementDescriptor implements Descriptor, Comparable<AvroReplacementDescriptor> {
042
043    @XNode("@forbidden")
044    protected String forbidden;
045
046    @XNode("@replacement")
047    protected String replacement;
048
049    @XNode("@priority")
050    protected int priority;
051
052    @Override
053    public int compareTo(AvroReplacementDescriptor o) {
054        return Integer.compare(priority, o.priority);
055    }
056
057    @Override
058    public boolean equals(Object obj) {
059        if (this == obj) {
060            return true;
061        }
062        if (obj == null) {
063            return false;
064        }
065        if (getClass() != obj.getClass()) {
066            return false;
067        }
068        AvroReplacementDescriptor other = (AvroReplacementDescriptor) obj;
069        return Objects.equals(forbidden, other.forbidden);
070    }
071
072    @Override
073    public String getId() {
074        return forbidden;
075    }
076
077    @Override
078    public int hashCode() {
079        final int prime = 31;
080        int result = 1;
081        result = prime * result + (forbidden == null ? 0 : forbidden.hashCode());
082        return result;
083    }
084
085}