001/*
002 * (C) Copyright 2020 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 *     bdelbosc
018 */
019package org.nuxeo.lib.stream.log;
020
021/**
022 * @since 11.1
023 */
024public class NameResolver {
025
026    protected final String prefix;
027
028    protected final int prefixLen;
029
030    public NameResolver(String prefix) {
031        this.prefix = (prefix != null) ? prefix : "";
032        this.prefixLen = this.prefix.length();
033    }
034
035    public String getId(Name name) {
036        return prefix + name.getId();
037    }
038
039    public Name getName(String id) {
040        if (!id.startsWith(prefix)) {
041            throw new IllegalArgumentException(String.format("invalid id: %s must starts with prefix %s", id, prefix));
042        }
043        return Name.ofId(id.substring(prefixLen));
044    }
045
046    public String getPrefix() {
047        return prefix;
048    }
049    @Override
050    public String toString() {
051        return "NameResolver{" + "prefix='" + prefix + '\'' + '}';
052    }
053}