/**
 * A class that contains examples of bit flags.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
@SuppressWarnings("checkstyle:EmptyLineSeparator")
public class Flags {
//[constants
    public static final int FOOD   =  1;
    public static final int SPELL  =  2;
    public static final int POTION =  4;
    public static final int WAND   =  8;
    public static final int WATER  = 16;   
    // And so on...
//]constants

    /**
     * A method to hold the fragments.
     *
     * @param args  The command line arguments
     */
    public static void main(String[] args) {
//[initialization
        int inventory;
        inventory = 0;
//]initialization

//[gotwater
        inventory = inventory | WATER;
//]gotwater
        System.out.println(inventory);
        
//[gotspell
        inventory |= SPELL;
//]gotspell
        System.out.println(inventory);

//[havewater
        boolean haveWater = (inventory & WATER) > 0;
//]havewater
        System.out.println("haveWater: " + haveWater);

//[havepotion
        boolean havePotion = (inventory & POTION) > 0;
//]havepotion
        System.out.println("havePotion: " + havePotion);

//[usewater
        inventory = inventory & (WATER ^ Integer.MAX_VALUE);
//]usewater
        haveWater = (inventory & WATER) > 0;
        System.out.println("After use: " + haveWater);
    }

//[allOf
    /**
     * Returns true if all of the bits in the parameter named needed are
     * set in the parameter named actual.
     *
     * @param needed  The flags that need to be 1
     * @param actual  The bits that are actually set
     * @return        true if all of the bits in named are set in actual
     */
    public static boolean allOf(int needed, int actual) {
        return (needed & actual) == needed;
    }
//]allOf

//[anyOf
    /**
     * Returns true if any of the bits in the parameter named needed are
     * set in the parameter named actual.
     *
     * @param needed  The flags that need to be 1
     * @param actual  The bits that are actually set
     * @return        true if any of the bits in named are set in actual
     */
    public static boolean anyOf(int needed, int actual) {
        return (needed & actual) > 0;
    }
//]anyOf
    
//[onlyOf
    /**
     * Returns true if exactly the bits in the parameter named needed are
     * set in the parameter named actual.
     *
     * @param needed  The flags that need to be 1
     * @param actual  The bits that are actually set
     * @return        true if only the bits in named are set in actual
     */
    public static boolean onlyOf(int needed, int actual) {
        return (needed == actual);
    }
//]onlyOf
}
