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 string value.
30   */
31  final class StringAuthentication
32      implements Authentication
33  {
34  
35      private final String key;
36  
37      private final String value;
38  
39      public StringAuthentication( String key, String value )
40      {
41          if ( key == null )
42          {
43              throw new IllegalArgumentException( "authentication key missing" );
44          }
45          this.key = key;
46          this.value = value;
47      }
48  
49      public void fill( AuthenticationContext context, String key, Map<String, String> data )
50      {
51          context.put( this.key, value );
52      }
53  
54      public void digest( AuthenticationDigest digest )
55      {
56          digest.update( key, value );
57      }
58  
59      @Override
60      public boolean equals( Object obj )
61      {
62          if ( this == obj )
63          {
64              return true;
65          }
66          if ( obj == null || !getClass().equals( obj.getClass() ) )
67          {
68              return false;
69          }
70          StringAuthentication that = (StringAuthentication) obj;
71          return eq( key, that.key ) && eq( value, that.value );
72      }
73  
74      private static <T> boolean eq( T s1, T s2 )
75      {
76          return s1 != null ? s1.equals( s2 ) : s2 == null;
77      }
78  
79      @Override
80      public int hashCode()
81      {
82          int hash = 17;
83          hash = hash * 31 + key.hashCode();
84          hash = hash * 31 + ( ( value != null ) ? value.hashCode() : 0 );
85          return hash;
86      }
87  
88      @Override
89      public String toString()
90      {
91          return key + "=" + value;
92      }
93  
94  }