View Javadoc
1   /*
2    * This file is part of dependency-check-core.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2014 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.utils;
19  
20  /**
21   * A generic pair of elements.
22   *
23   * @param <L> the type for the left element in the pair
24   * @param <R> the type for the right element in the pair
25   *
26   * @author Jeremy Long
27   */
28  public class Pair<L, R> {
29  
30      /**
31       * Constructs a new empty pair.
32       */
33      public Pair() {
34      }
35  
36      /**
37       * Constructs a new Pair with the given left and right values.
38       *
39       * @param left the value for the left pair
40       * @param right the value for the right pair
41       */
42      public Pair(L left, R right) {
43          this.left = left;
44          this.right = right;
45      }
46      /**
47       * The left element of the pair.
48       */
49      private L left = null;
50  
51      /**
52       * Get the value of left.
53       *
54       * @return the value of left
55       */
56      public L getLeft() {
57          return left;
58      }
59  
60      /**
61       * Set the value of left.
62       *
63       * @param left new value of left
64       */
65      public void setLeft(L left) {
66          this.left = left;
67      }
68      /**
69       * The right element of the pair.
70       */
71      private R right = null;
72  
73      /**
74       * Get the value of right.
75       *
76       * @return the value of right
77       */
78      public R getRight() {
79          return right;
80      }
81  
82      /**
83       * Set the value of right.
84       *
85       * @param right new value of right
86       */
87      public void setRight(R right) {
88          this.right = right;
89      }
90  
91      /**
92       * Generates the hash code using the hash codes from the contained objects.
93       *
94       * @return the hash code of the Pair
95       */
96      @Override
97      public int hashCode() {
98          int hash = 3;
99          hash = 53 * hash + (this.left != null ? this.left.hashCode() : 0);
100         hash = 53 * hash + (this.right != null ? this.right.hashCode() : 0);
101         return hash;
102     }
103 
104     /**
105      * Determines the equality of this and the provided object.
106      *
107      * @param obj the {@link Object} to check for equality to this
108      * @return true if this and the provided {@link Object} are equal; otherwise false
109      */
110     @Override
111     public boolean equals(Object obj) {
112         if (obj == null) {
113             return false;
114         }
115         if (getClass() != obj.getClass()) {
116             return false;
117         }
118         final Pair<?, ?> other = (Pair<?, ?>) obj;
119         if (this.left != other.left && (this.left == null || !this.left.equals(other.left))) {
120             return false;
121         }
122         if (this.right != other.right && (this.right == null || !this.right.equals(other.right))) {
123             return false;
124         }
125         return true;
126     }
127 }