001/*
002 * Forge Mod Loader
003 * Copyright (c) 2012-2013 cpw.
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser Public License v2.1
006 * which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
008 * 
009 * Contributors:
010 *     cpw - implementation
011 */
012
013package cpw.mods.fml.common.versioning;
014
015/*
016 * Licensed to the Apache Software Foundation (ASF) under one
017 * or more contributor license agreements.  See the NOTICE file
018 * distributed with this work for additional information
019 * regarding copyright ownership.  The ASF licenses this file
020 * to you under the Apache License, Version 2.0 (the
021 * "License"); you may not use this file except in compliance
022 * with the License.  You may obtain a copy of the License at
023 *
024 *  http://www.apache.org/licenses/LICENSE-2.0
025 *
026 * Unless required by applicable law or agreed to in writing,
027 * software distributed under the License is distributed on an
028 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
029 * KIND, either express or implied.  See the License for the
030 * specific language governing permissions and limitations
031 * under the License.
032 */
033
034/**
035 * Describes a restriction in versioning.
036 *
037 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
038 */
039public class Restriction
040{
041    private final ArtifactVersion lowerBound;
042
043    private final boolean lowerBoundInclusive;
044
045    private final ArtifactVersion upperBound;
046
047    private final boolean upperBoundInclusive;
048
049    public static final Restriction EVERYTHING = new Restriction( null, false, null, false );
050
051    public Restriction( ArtifactVersion lowerBound, boolean lowerBoundInclusive, ArtifactVersion upperBound,
052                        boolean upperBoundInclusive )
053    {
054        this.lowerBound = lowerBound;
055        this.lowerBoundInclusive = lowerBoundInclusive;
056        this.upperBound = upperBound;
057        this.upperBoundInclusive = upperBoundInclusive;
058    }
059
060    public ArtifactVersion getLowerBound()
061    {
062        return lowerBound;
063    }
064
065    public boolean isLowerBoundInclusive()
066    {
067        return lowerBoundInclusive;
068    }
069
070    public ArtifactVersion getUpperBound()
071    {
072        return upperBound;
073    }
074
075    public boolean isUpperBoundInclusive()
076    {
077        return upperBoundInclusive;
078    }
079
080    public boolean containsVersion( ArtifactVersion version )
081    {
082        if ( lowerBound != null )
083        {
084            int comparison = lowerBound.compareTo( version );
085
086            if ( ( comparison == 0 ) && !lowerBoundInclusive )
087            {
088                return false;
089            }
090            if ( comparison > 0 )
091            {
092                return false;
093            }
094        }
095        if ( upperBound != null )
096        {
097            int comparison = upperBound.compareTo( version );
098
099            if ( ( comparison == 0 ) && !upperBoundInclusive )
100            {
101                return false;
102            }
103            if ( comparison < 0 )
104            {
105                return false;
106            }
107        }
108
109        return true;
110    }
111
112    @Override
113    public int hashCode()
114    {
115        int result = 13;
116
117        if ( lowerBound == null )
118        {
119            result += 1;
120        }
121        else
122        {
123            result += lowerBound.hashCode();
124        }
125
126        result *= lowerBoundInclusive ? 1 : 2;
127
128        if ( upperBound == null )
129        {
130            result -= 3;
131        }
132        else
133        {
134            result -= upperBound.hashCode();
135        }
136
137        result *= upperBoundInclusive ? 2 : 3;
138
139        return result;
140    }
141
142    @Override
143    public boolean equals( Object other )
144    {
145        if ( this == other )
146        {
147            return true;
148        }
149
150        if ( !( other instanceof Restriction ) )
151        {
152            return false;
153        }
154
155        Restriction restriction = (Restriction) other;
156        if ( lowerBound != null )
157        {
158            if ( !lowerBound.equals( restriction.lowerBound ) )
159            {
160                return false;
161            }
162        }
163        else if ( restriction.lowerBound != null )
164        {
165            return false;
166        }
167
168        if ( lowerBoundInclusive != restriction.lowerBoundInclusive )
169        {
170            return false;
171        }
172
173        if ( upperBound != null )
174        {
175            if ( !upperBound.equals( restriction.upperBound ) )
176            {
177                return false;
178            }
179        }
180        else if ( restriction.upperBound != null )
181        {
182            return false;
183        }
184
185        if ( upperBoundInclusive != restriction.upperBoundInclusive )
186        {
187            return false;
188        }
189
190        return true;
191    }
192
193    public String toString()
194    {
195        StringBuilder buf = new StringBuilder();
196
197        buf.append( isLowerBoundInclusive() ? "[" : "(" );
198        if ( getLowerBound() != null )
199        {
200            buf.append( getLowerBound().toString() );
201        }
202        buf.append( "," );
203        if ( getUpperBound() != null )
204        {
205            buf.append( getUpperBound().toString() );
206        }
207        buf.append( isUpperBoundInclusive() ? "]" : ")" );
208
209        return buf.toString();
210    }
211}