View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.transport.minio;
20  
21  import java.util.Objects;
22  
23  import static java.util.Objects.requireNonNull;
24  
25  /**
26   * S3 Object name, bucket + name.
27   *
28   * @since 2.0.2
29   */
30  public final class ObjectName {
31      private final String bucket;
32      private final String name;
33      private final int hashCode;
34  
35      public ObjectName(String bucket, String name) {
36          this.bucket = requireNonNull(bucket);
37          this.name = requireNonNull(name);
38  
39          if (bucket.contains("/")) {
40              throw new IllegalArgumentException("invalid bucket name: " + bucket);
41          }
42          if (name.contains("\\")) {
43              throw new IllegalArgumentException("invalid object name: " + name);
44          }
45  
46          this.hashCode = Objects.hash(bucket, name);
47      }
48  
49      public String getBucket() {
50          return bucket;
51      }
52  
53      public String getName() {
54          return name;
55      }
56  
57      @Override
58      public boolean equals(Object o) {
59          if (this == o) {
60              return true;
61          }
62          if (o == null || getClass() != o.getClass()) {
63              return false;
64          }
65          ObjectName that = (ObjectName) o;
66          return Objects.equals(bucket, that.bucket) && Objects.equals(name, that.name);
67      }
68  
69      @Override
70      public int hashCode() {
71          return hashCode;
72      }
73  
74      @Override
75      public String toString() {
76          return bucket + "/" + name;
77      }
78  
79      public static String normalize(String name) {
80          return name.replace('\\', '/');
81      }
82  }