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  
24  import org.eclipse.aether.repository.Authentication;
25  import org.eclipse.aether.repository.AuthenticationContext;
26  import org.eclipse.aether.repository.AuthenticationDigest;
27  
28  /**
29   * Authentication block that manages a single authentication key and its component value. In this context, component
30   * refers to an object whose behavior is solely dependent on its implementation class.
31   */
32  final class ComponentAuthentication
33      implements Authentication
34  {
35  
36      private final String key;
37  
38      private final Object value;
39  
40      public ComponentAuthentication( String key, Object value )
41      {
42          if ( key == null )
43          {
44              throw new IllegalArgumentException( "authentication key missing" );
45          }
46          this.key = key;
47          this.value = value;
48      }
49  
50      public void fill( AuthenticationContext context, String key, Map<String, String> data )
51      {
52          context.put( this.key, value );
53      }
54  
55      public void digest( AuthenticationDigest digest )
56      {
57          if ( value != null )
58          {
59              digest.update( key, value.getClass().getName() );
60          }
61      }
62  
63      @Override
64      public boolean equals( Object obj )
65      {
66          if ( this == obj )
67          {
68              return true;
69          }
70          if ( obj == null || !getClass().equals( obj.getClass() ) )
71          {
72              return false;
73          }
74          ComponentAuthentication that = (ComponentAuthentication) obj;
75          return key.equals( that.key ) && eqClass( value, that.value );
76      }
77  
78      private static <T> boolean eqClass( T s1, T s2 )
79      {
80          return ( s1 == null ) ? s2 == null : s2 != null && s1.getClass().equals( s2.getClass() );
81      }
82  
83      @Override
84      public int hashCode()
85      {
86          int hash = 17;
87          hash = hash * 31 + key.hashCode();
88          hash = hash * 31 + ( ( value != null ) ? value.getClass().hashCode() : 0 );
89          return hash;
90      }
91  
92      @Override
93      public String toString()
94      {
95          return key + "=" + value;
96      }
97  
98  }