    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.validator.util;
    018
    019import java.io.Serializable;
    020
    021/**
    022 * Represents a collection of 64 boolean (on/off) flags.  Individual flags
    023 * are represented by powers of 2.  For example,<br>
    024 * Flag 1 = 1<br>
    025 * Flag 2 = 2<br>
    026 * Flag 3 = 4<br>
    027 * Flag 4 = 8<br><br>
    028 * or using shift operator to make numbering easier:<br>
    029 * Flag 1 = 1 &lt;&lt; 0<br>
    030 * Flag 2 = 1 &lt;&lt; 1<br>
    031 * Flag 3 = 1 &lt;&lt; 2<br>
    032 * Flag 4 = 1 &lt;&lt; 3<br>
    033 *
    034 * <p>
    035 * There cannot be a flag with a value of 3 because that represents Flag 1
    036 * and Flag 2 both being on/true.
    037 * </p>
    038 */
    039public class Flags implements Serializable, Cloneable {
    040
    041    private static final long serialVersionUID = 8481587558770237995L;
    042
    043    /**
    044     * Represents the current flag state.
    045     */
    046    private long flags;
    047
    048    /**
    049     * Create a new Flags object.
    050     */
    051    public Flags() {
    052    }
    053
    054    /**
    055     * Initialize a new Flags object with the given flags.
    056     *
    057     * @param flags collection of boolean flags to represent.
    058     */
    059    public Flags(final long flags) {
    060        this.flags = flags;
    061    }
    062
    063    /**
    064     * Turn off all flags.  This is a synonym for {@code turnOffAll()}.
    065     * @since 1.1.1
    066     */
    067    public void clear() {
    068        flags = 0;
    069    }
    070
    071    /**
    072     * Clone this Flags object.
    073     *
    074     * @return a copy of this object.
    075     * @see Object#clone()
    076     */
    077    @Override
    078    public Object clone() {
    079        try {
    080            return super.clone();
    081        } catch (final CloneNotSupportedException e) {
    082            throw new UnsupportedOperationException("Couldn't clone Flags object.", e);
    083        }
    084    }
    085
    086    /**
    087     * Tests if two Flags objects are in the same state.
    088     * @param obj object being tested
    089     * @see Object#equals(Object)
    090     * @return whether the objects are equal.
    091     */
    092    @Override
    093    public boolean equals(final Object obj) {
    094        if (this == obj) {
    095            return true;
    096        }
    097        if (!(obj instanceof Flags)) {
    098            return false;
    099        }
    100        final Flags other = (Flags) obj;
    101        return flags == other.flags;
    102    }
    103
    104    /**
    105     * Returns the current flags.
    106     *
    107     * @return collection of boolean flags represented.
    108     */
    109    public long getFlags() {
    110        return flags;
    111    }
    112
    113    /**
    114     * The hash code is based on the current state of the flags.
    115     * @see Object#hashCode()
    116     * @return the hash code for this object.
    117     */
    118    @Override
    119    public int hashCode() {
    120        return (int) flags;
    121    }
    122
    123    /**
    124     * Tests whether the given flag is off. If the flag is not a power of 2
    125     * (for example, 3) this tests whether the combination of flags is off.
    126     *
    127     * @param flag Flag value to check.
    128     * @return whether the specified flag value is off.
    129     */
    130    public boolean isOff(final long flag) {
    131        return (flags & flag) == 0;
    132    }
    133
    134    /**
    135     * Tests whether the given flag is on. If the flag is not a power of 2
    136     * (for example, 3) this tests whether the combination of flags is on.
    137     *
    138     * @param flag Flag value to check.
    139     * @return whether the specified flag value is on.
    140     */
    141    public boolean isOn(final long flag) {
    142        return (flags & flag) == flag;
    143    }
    144
    145    /**
    146     * Returns a 64 length String with the first flag on the right and the
    147     * 64th flag on the left. A 1 indicates the flag is on, a 0 means it's
    148     * off.
    149     *
    150     * @return string representation of this object.
    151     */
    152    @Override
    153    public String toString() {
    154        final StringBuilder bin = new StringBuilder(Long.toBinaryString(flags));
    155        for (int i = 64 - bin.length(); i > 0; i--) { // CHECKSTYLE IGNORE MagicNumber
    156            bin.insert(0, "0");
    157        }
    158        return bin.toString();
    159    }
    160
    161    /**
    162     * Turns off the given flag. If the flag is not a power of 2 (for example, 3) this
    163     * turns off multiple flags.
    164     *
    165     * @param flag Flag value to turn off.
    166     */
    167    public void turnOff(final long flag) {
    168        flags &= ~flag;
    169    }
    170
    171    /**
    172     * Turn off all flags.
    173     */
    174    public void turnOffAll() {
    175        flags = 0;
    176    }
    177
    178    /**
    179     * Turns on the given flag. If the flag is not a power of 2 (for example, 3) this
    180     * turns on multiple flags.
    181     *
    182     * @param flag Flag value to turn on.
    183     */
    184    public void turnOn(final long flag) {
    185        flags |= flag;
    186    }
    187
    188    /**
    189     * Turn on all 64 flags.
    190     */
    191    public void turnOnAll() {
    192        flags = 0xFFFFFFFFFFFFFFFFL;
    193    }
    194
    195}