001/****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one   *
003 * or more contributor license agreements.  See the NOTICE file *
004 * distributed with this work for additional information        *
005 * regarding copyright ownership.  The ASF licenses this file   *
006 * to you under the Apache License, Version 2.0 (the            *
007 * "License"); you may not use this file except in compliance   *
008 * with the License.  You may obtain a copy of the License at   *
009 *                                                              *
010 *   http://www.apache.org/licenses/LICENSE-2.0                 *
011 *                                                              *
012 * Unless required by applicable law or agreed to in writing,   *
013 * software distributed under the License is distributed on an  *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015 * KIND, either express or implied.  See the License for the    *
016 * specific language governing permissions and limitations      *
017 * under the License.                                           *
018 ****************************************************************/
019package org.apache.james.mailbox.maildir;
020
021import java.util.LinkedList;
022
023public class UidConstraint {
024    
025    private LinkedList<Constraint> constraints = new LinkedList<Constraint>();
026    
027    public UidConstraint append(Constraint constraint) {
028        constraints.add(constraint);
029        return this;
030    }
031    
032    public UidConstraint equals(long uid) {
033        constraints.add(new Equals(uid));
034        return this;
035    }
036    
037    public UidConstraint lessOrEquals(long uid) {
038        constraints.add(new LessOrEquals(uid));
039        return this;
040    }
041    
042    public UidConstraint greaterOrEquals(long uid) {
043        constraints.add(new GreaterOrEquals(uid));
044        return this;
045    }
046    
047    public UidConstraint between(long lower, long upper) {
048        constraints.add(new Between(lower, upper));
049        return this;
050    }
051    
052    public boolean isAllowed(long uid) {
053        for (Constraint constraint : constraints)
054            if (!constraint.isAllowed(uid))
055                return false;
056        return true;
057    }
058
059    public abstract static class Constraint {
060        
061        public abstract boolean isAllowed(long uid);
062        
063    }
064    
065    public static class Equals extends Constraint {
066
067        private long uid;
068        
069        public Equals(long uid) {
070            this.uid = uid;
071        }
072        
073        @Override
074        public boolean isAllowed(long uid) {
075            return this.uid == uid;
076        }
077        
078    }
079    
080    public static class LessOrEquals extends Constraint {
081
082        private long uid;
083        
084        public LessOrEquals(long uid) {
085            this.uid = uid;
086        }
087
088        @Override
089        public boolean isAllowed(long uid) {
090            return uid <= this.uid;
091        }
092        
093    }
094    
095    public static class GreaterOrEquals extends Constraint {
096
097        private long uid;
098        
099        public GreaterOrEquals(long uid) {
100            this.uid = uid;
101        }
102
103        @Override
104        public boolean isAllowed(long uid) {
105            return uid >= this.uid;
106        }
107        
108    }
109    
110    public static class Between extends Constraint {
111        
112        private long lower;
113        private long upper;
114        
115        public Between(long lower, long upper) {
116            this.lower = lower;
117            this.upper = upper;
118        }
119
120        @Override
121        public boolean isAllowed(long uid) {
122            return (uid >= lower) && (uid <= upper);
123        }
124    }
125    
126}