| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Restriction |
|
| 4.555555555555555;4.556 |
| 1 | package org.apache.maven.artifact.versioning; | |
| 2 | ||
| 3 | ||
| 4 | /* | |
| 5 | * Licensed to the Apache Software Foundation (ASF) under one | |
| 6 | * or more contributor license agreements. See the NOTICE file | |
| 7 | * distributed with this work for additional information | |
| 8 | * regarding copyright ownership. The ASF licenses this file | |
| 9 | * to you under the Apache License, Version 2.0 (the | |
| 10 | * "License"); you may not use this file except in compliance | |
| 11 | * with the License. You may obtain a copy of the License at | |
| 12 | * | |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 14 | * | |
| 15 | * Unless required by applicable law or agreed to in writing, | |
| 16 | * software distributed under the License is distributed on an | |
| 17 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| 18 | * KIND, either express or implied. See the License for the | |
| 19 | * specific language governing permissions and limitations | |
| 20 | * under the License. | |
| 21 | */ | |
| 22 | ||
| 23 | /** | |
| 24 | * Describes a restriction in versioning. | |
| 25 | * | |
| 26 | * @author <a href="mailto:brett@apache.org">Brett Porter</a> | |
| 27 | * @version $Id: Restriction.java 640549 2008-03-24 20:05:11Z bentmann $ | |
| 28 | */ | |
| 29 | public class Restriction | |
| 30 | { | |
| 31 | private final ArtifactVersion lowerBound; | |
| 32 | ||
| 33 | private final boolean lowerBoundInclusive; | |
| 34 | ||
| 35 | private final ArtifactVersion upperBound; | |
| 36 | ||
| 37 | private final boolean upperBoundInclusive; | |
| 38 | ||
| 39 | 0 | static final Restriction EVERYTHING = new Restriction( null, false, null, false ); |
| 40 | ||
| 41 | public Restriction( ArtifactVersion lowerBound, boolean lowerBoundInclusive, ArtifactVersion upperBound, | |
| 42 | boolean upperBoundInclusive ) | |
| 43 | 0 | { |
| 44 | 0 | this.lowerBound = lowerBound; |
| 45 | 0 | this.lowerBoundInclusive = lowerBoundInclusive; |
| 46 | 0 | this.upperBound = upperBound; |
| 47 | 0 | this.upperBoundInclusive = upperBoundInclusive; |
| 48 | 0 | } |
| 49 | ||
| 50 | public ArtifactVersion getLowerBound() | |
| 51 | { | |
| 52 | 0 | return lowerBound; |
| 53 | } | |
| 54 | ||
| 55 | public boolean isLowerBoundInclusive() | |
| 56 | { | |
| 57 | 0 | return lowerBoundInclusive; |
| 58 | } | |
| 59 | ||
| 60 | public ArtifactVersion getUpperBound() | |
| 61 | { | |
| 62 | 0 | return upperBound; |
| 63 | } | |
| 64 | ||
| 65 | public boolean isUpperBoundInclusive() | |
| 66 | { | |
| 67 | 0 | return upperBoundInclusive; |
| 68 | } | |
| 69 | ||
| 70 | public boolean containsVersion( ArtifactVersion version ) | |
| 71 | { | |
| 72 | 0 | if ( lowerBound != null ) |
| 73 | { | |
| 74 | 0 | int comparison = lowerBound.compareTo( version ); |
| 75 | 0 | if ( comparison == 0 && !lowerBoundInclusive ) |
| 76 | { | |
| 77 | 0 | return false; |
| 78 | } | |
| 79 | 0 | if ( comparison > 0 ) |
| 80 | { | |
| 81 | 0 | return false; |
| 82 | } | |
| 83 | } | |
| 84 | 0 | if ( upperBound != null ) |
| 85 | { | |
| 86 | 0 | int comparison = upperBound.compareTo( version ); |
| 87 | 0 | if ( comparison == 0 && !upperBoundInclusive ) |
| 88 | { | |
| 89 | 0 | return false; |
| 90 | } | |
| 91 | 0 | if ( comparison < 0 ) |
| 92 | { | |
| 93 | 0 | return false; |
| 94 | } | |
| 95 | } | |
| 96 | 0 | return true; |
| 97 | } | |
| 98 | ||
| 99 | ||
| 100 | public int hashCode() | |
| 101 | { | |
| 102 | 0 | int result = 13; |
| 103 | ||
| 104 | 0 | if ( lowerBound == null ) |
| 105 | { | |
| 106 | 0 | result += 1; |
| 107 | } | |
| 108 | else | |
| 109 | { | |
| 110 | 0 | result += lowerBound.hashCode(); |
| 111 | } | |
| 112 | ||
| 113 | 0 | result *= lowerBoundInclusive ? 1 : 2; |
| 114 | ||
| 115 | 0 | if ( upperBound == null ) |
| 116 | { | |
| 117 | 0 | result -= 3; |
| 118 | } | |
| 119 | else | |
| 120 | { | |
| 121 | 0 | result -= upperBound.hashCode(); |
| 122 | } | |
| 123 | ||
| 124 | 0 | result *= upperBoundInclusive ? 2 : 3; |
| 125 | ||
| 126 | 0 | return result; |
| 127 | } | |
| 128 | ||
| 129 | public boolean equals( Object other ) | |
| 130 | { | |
| 131 | 0 | if ( this == other ) |
| 132 | { | |
| 133 | 0 | return true; |
| 134 | } | |
| 135 | ||
| 136 | 0 | if ( !(other instanceof Restriction ) ) |
| 137 | { | |
| 138 | 0 | return false; |
| 139 | } | |
| 140 | ||
| 141 | 0 | Restriction restriction = (Restriction) other; |
| 142 | 0 | if ( lowerBound != null ) |
| 143 | { | |
| 144 | 0 | if ( !lowerBound.equals( restriction.lowerBound ) ) |
| 145 | { | |
| 146 | 0 | return false; |
| 147 | } | |
| 148 | } | |
| 149 | 0 | else if ( restriction.lowerBound != null ) |
| 150 | { | |
| 151 | 0 | return false; |
| 152 | } | |
| 153 | ||
| 154 | 0 | if ( lowerBoundInclusive != restriction.lowerBoundInclusive ) |
| 155 | { | |
| 156 | 0 | return false; |
| 157 | } | |
| 158 | ||
| 159 | 0 | if ( upperBound != null ) |
| 160 | { | |
| 161 | 0 | if ( !upperBound.equals( restriction.upperBound ) ) |
| 162 | { | |
| 163 | 0 | return false; |
| 164 | } | |
| 165 | } | |
| 166 | 0 | else if ( restriction.upperBound != null ) |
| 167 | { | |
| 168 | 0 | return false; |
| 169 | } | |
| 170 | ||
| 171 | 0 | if ( upperBoundInclusive != restriction.upperBoundInclusive ) |
| 172 | { | |
| 173 | 0 | return false; |
| 174 | } | |
| 175 | ||
| 176 | 0 | return true; |
| 177 | } | |
| 178 | ||
| 179 | public String toString() | |
| 180 | { | |
| 181 | 0 | StringBuffer buf = new StringBuffer(); |
| 182 | ||
| 183 | 0 | buf.append( isLowerBoundInclusive() ? "[" : "(" ); |
| 184 | 0 | if ( getLowerBound() != null ) |
| 185 | { | |
| 186 | 0 | buf.append( getLowerBound().toString() ); |
| 187 | } | |
| 188 | 0 | buf.append( "," ); |
| 189 | 0 | if ( getUpperBound() != null ) |
| 190 | { | |
| 191 | 0 | buf.append( getUpperBound().toString() ); |
| 192 | } | |
| 193 | 0 | buf.append( isUpperBoundInclusive() ? "]" : ")" ); |
| 194 | ||
| 195 | 0 | return buf.toString(); |
| 196 | } | |
| 197 | } |