001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *   Unless required by applicable law or agreed to in writing,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020
021package org.apache.james.rrt.lib;
022
023import java.io.Serializable;
024
025import org.apache.james.rrt.api.RecipientRewriteTable;
026
027import com.google.common.base.MoreObjects;
028import com.google.common.base.Objects;
029import com.google.common.base.Preconditions;
030
031
032public class MappingImpl implements Mapping, Serializable {
033
034    private static final long serialVersionUID = 1L;
035
036    private static final String ADDRESS_PREFIX = "";
037
038
039    public static MappingImpl of(String mapping) {
040        return new MappingImpl("", mapping);
041    }
042    
043    public static MappingImpl address(String mapping) {
044        return new MappingImpl(ADDRESS_PREFIX, mapping);
045    }
046
047    public static MappingImpl regex(String mapping) {
048        return new MappingImpl(RecipientRewriteTable.REGEX_PREFIX, mapping);
049    }
050
051    public static MappingImpl error(String mapping) {
052        return new MappingImpl(RecipientRewriteTable.ERROR_PREFIX, mapping);
053    }
054
055    public static MappingImpl domain(String mapping) {
056        return new MappingImpl(RecipientRewriteTable.ALIASDOMAIN_PREFIX, mapping);
057    }
058    
059    private final String mapping;
060
061    private MappingImpl(String prefix, String mapping) {
062        Preconditions.checkNotNull(mapping);
063        this.mapping = prefix + mapping;
064    }
065    
066    @Override
067    public String asString() {
068        return mapping;
069    }
070    
071    @Override
072    public boolean hasDomain() {
073        return mapping.contains("@");
074    }
075    
076    @Override
077    public Mapping appendDomain(String domain) {
078        Preconditions.checkNotNull(domain);
079        return new MappingImpl("", mapping + "@" + domain);
080    }
081    
082    @Override
083    public Type getType() {
084        if (mapping.startsWith(RecipientRewriteTable.ALIASDOMAIN_PREFIX)) {
085            return Type.Domain;
086        } else if (mapping.startsWith(RecipientRewriteTable.REGEX_PREFIX)) {
087            return Type.Regex;
088        } else if (mapping.startsWith(RecipientRewriteTable.ERROR_PREFIX)) {
089            return Type.Error;
090        } else {
091            return Type.Address;
092        }
093    }
094    
095    @Override
096    public String getErrorMessage() {
097        Preconditions.checkState(mapping.startsWith(RecipientRewriteTable.ERROR_PREFIX));
098        return mapping.substring(RecipientRewriteTable.ERROR_PREFIX.length());
099    }
100    
101    @Override
102    public boolean equals(Object other) {
103        if (other instanceof MappingImpl) {
104            MappingImpl otherMapping = (MappingImpl) other;
105            return Objects.equal(mapping, otherMapping.mapping);
106        }
107        return false;
108    }
109    
110    @Override
111    public int hashCode() {
112        return Objects.hashCode(mapping);
113    }
114    
115    @Override
116    public String toString() {
117        return MoreObjects.toStringHelper(getClass()).add("mapping", mapping).toString();
118    }
119    
120}