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 */
019package org.eclipse.aether.transport.minio;
020
021import java.util.Objects;
022
023import static java.util.Objects.requireNonNull;
024
025/**
026 * S3 Object name, bucket + name.
027 *
028 * @since 2.0.2
029 */
030public final class ObjectName {
031    private final String bucket;
032    private final String name;
033    private final int hashCode;
034
035    public ObjectName(String bucket, String name) {
036        this.bucket = requireNonNull(bucket);
037        this.name = requireNonNull(name);
038
039        if (bucket.contains("/")) {
040            throw new IllegalArgumentException("invalid bucket name: " + bucket);
041        }
042        if (name.contains("\\")) {
043            throw new IllegalArgumentException("invalid object name: " + name);
044        }
045
046        this.hashCode = Objects.hash(bucket, name);
047    }
048
049    public String getBucket() {
050        return bucket;
051    }
052
053    public String getName() {
054        return name;
055    }
056
057    @Override
058    public boolean equals(Object o) {
059        if (this == o) {
060            return true;
061        }
062        if (o == null || getClass() != o.getClass()) {
063            return false;
064        }
065        ObjectName that = (ObjectName) o;
066        return Objects.equals(bucket, that.bucket) && Objects.equals(name, that.name);
067    }
068
069    @Override
070    public int hashCode() {
071        return hashCode;
072    }
073
074    @Override
075    public String toString() {
076        return bucket + "/" + name;
077    }
078
079    public static String normalize(String name) {
080        return name.replace('\\', '/');
081    }
082}