View Javadoc
1   package org.eclipse.aether.util.repository;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Map;
23  import static java.util.Objects.requireNonNull;
24  
25  import org.eclipse.aether.repository.Authentication;
26  import org.eclipse.aether.repository.AuthenticationContext;
27  import org.eclipse.aether.repository.AuthenticationDigest;
28  
29  /**
30   * Authentication block that manages a single authentication key and its component value. In this context, component
31   * refers to an object whose behavior is solely dependent on its implementation class.
32   */
33  final class ComponentAuthentication
34      implements Authentication
35  {
36  
37      private final String key;
38  
39      private final Object value;
40  
41      ComponentAuthentication( String key, Object value )
42      {
43          this.key = requireNonNull( key, "authentication key cannot be null" );
44          if ( key.length() == 0 )
45          {
46              throw new IllegalArgumentException( "authentication key cannot be empty" );
47          }
48          this.value = value;
49      }
50  
51      public void fill( AuthenticationContext context, String key, Map<String, String> data )
52      {
53          context.put( this.key, value );
54      }
55  
56      public void digest( AuthenticationDigest digest )
57      {
58          if ( value != null )
59          {
60              digest.update( key, value.getClass().getName() );
61          }
62      }
63  
64      @Override
65      public boolean equals( Object obj )
66      {
67          if ( this == obj )
68          {
69              return true;
70          }
71          if ( obj == null || !getClass().equals( obj.getClass() ) )
72          {
73              return false;
74          }
75          ComponentAuthentication that = (ComponentAuthentication) obj;
76          return key.equals( that.key ) && eqClass( value, that.value );
77      }
78  
79      private static <T> boolean eqClass( T s1, T s2 )
80      {
81          return ( s1 == null ) ? s2 == null : s2 != null && s1.getClass().equals( s2.getClass() );
82      }
83  
84      @Override
85      public int hashCode()
86      {
87          int hash = 17;
88          hash = hash * 31 + key.hashCode();
89          hash = hash * 31 + ( ( value != null ) ? value.getClass().hashCode() : 0 );
90          return hash;
91      }
92  
93      @Override
94      public String toString()
95      {
96          return key + "=" + value;
97      }
98  
99  }