Android Open Source - Airplanes Randomized Queue






From Project

Back to project page Airplanes.

License

The source code is released under:

GNU General Public License

If you think the Android project Airplanes listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.axnsan.airplanes.util;
//  www .j  a  v  a2s. com
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Random;


public class RandomizedQueue<Item> implements Iterable<Item> {
    private Item[] items = null;
    private int size = 0;
    private static Random random = new Random();
    
    @SuppressWarnings("unchecked")
    public RandomizedQueue() {
        items = (Item[]) new Object[1];
    }
    
    public boolean isEmpty() {
        return size == 0;
    }
    
    public int size() {
        return size;
    }
    
    /**Double the array's capacity*/
    @SuppressWarnings("unchecked")
    private void doubleCapacity() {
        if (items.length == 0) {
            items = (Item[]) new Object[1];
            return;
        }
        Item[] tmp = (Item[]) new Object[items.length*2];
        for (int i = 0;i < size;++i) {
                tmp[i] = items[i];
            items[i] = null;
        }
        items = tmp;
    }
    
    /**Halve the array's capacity and clear the garbage*/
    @SuppressWarnings("unchecked")
    private void halveCapacity() {
        if (size > items.length/2)
            throw new RuntimeException("Cannot halve the array if it is more than half filled");
        
        Item[] tmp = (Item[]) new Object[items.length/2];
        for (int i = 0;i < size;++i) {
            if (items[i] != null)
                tmp[i] = items[i];
            items[i] = null;
        }
        items = tmp;
    }
    
    /**Add an item*/
    public void push(Item item) {
        if (item == null)
            throw new NullPointerException();
        
        if (size >= items.length)
            doubleCapacity();
        
        items[size++] = item;
    }
    
    /**Remove and return a random item*/
    public Item pop() {
        if (isEmpty())
            throw new NoSuchElementException();
        
        int index = random.nextInt(size);
        Item ret = items[index];
        items[index] = items[size-1];
        items[size-1] = null;
        --size;
        
        if (size <= items.length/4)
            halveCapacity();
        
        return ret;
    }
    
    /**Return a random item without removing*/
    public Item peek() {
        if (isEmpty())
            throw new NoSuchElementException();
        
        return items[random.nextInt(size)];
    }
    
    private static void shuffle(int[] array)
    {
        int index, temp;
        for (int i = array.length - 1; i > 0; i--)
        {
            index = random.nextInt(i + 1);
            temp = array[index];
            array[index] = array[i];
            array[i] = temp;
        }
    }
    
    private class RandomIterator implements Iterator<Item> {
        private int[] sequence;
        private int pos = 0;
        
        public RandomIterator() {
            sequence = new int[size];
            for (int i = 0;i < size;++i) {
                sequence[i] = i;
            }
            shuffle(sequence);
        }
        @Override
        public boolean hasNext() {
            return pos < sequence.length;
        }

        @Override
        public Item next() {
            if (!hasNext())
                throw new NoSuchElementException();
            
            return items[sequence[pos++]];
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    }
    /**Return an iterator in random order*/
    public Iterator<Item> iterator() {
        return new RandomIterator();
    }
}




Java Source Code List

com.axnsan.airplanes.Airplanes.java
com.axnsan.airplanes.BaseGrid.java
com.axnsan.airplanes.GameConfiguration.java
com.axnsan.airplanes.GameState.java
com.axnsan.airplanes.GridRandomizer.java
com.axnsan.airplanes.Grid.java
com.axnsan.airplanes.GuardedScreen.java
com.axnsan.airplanes.HotseatMatchHandler.java
com.axnsan.airplanes.MainActivity.java
com.axnsan.airplanes.Main.java
com.axnsan.airplanes.MatchHandler.java
com.axnsan.airplanes.PlacementGrid.java
com.axnsan.airplanes.Plane.java
com.axnsan.airplanes.Player.java
com.axnsan.airplanes.PlayingGrid.java
com.axnsan.airplanes.online.ClientSocket.java
com.axnsan.airplanes.online.EventHandler.java
com.axnsan.airplanes.online.Game.java
com.axnsan.airplanes.online.LobbyScreen.java
com.axnsan.airplanes.online.LoginScreen.java
com.axnsan.airplanes.online.Message.java
com.axnsan.airplanes.online.Messages.java
com.axnsan.airplanes.online.OnlineGameScreen.java
com.axnsan.airplanes.online.OnlineMatchHandler.java
com.axnsan.airplanes.online.OnlineMenuScreen.java
com.axnsan.airplanes.online.OnlineSettingsScreen.java
com.axnsan.airplanes.online.SessionData.java
com.axnsan.airplanes.screens.BeginTurnScreen.java
com.axnsan.airplanes.screens.HotseatPlayersScreen.java
com.axnsan.airplanes.screens.HotseatScreen.java
com.axnsan.airplanes.screens.HotseatSettingsScreen.java
com.axnsan.airplanes.screens.HotseatStartScreen.java
com.axnsan.airplanes.screens.MainMenuScreen.java
com.axnsan.airplanes.screens.PlacementScreen.java
com.axnsan.airplanes.screens.PlayMenuScreen.java
com.axnsan.airplanes.screens.PracticeScreen.java
com.axnsan.airplanes.screens.PracticeSettingsScreen.java
com.axnsan.airplanes.screens.PracticeStartScreen.java
com.axnsan.airplanes.util.ActionManager.java
com.axnsan.airplanes.util.ActionResolver.java
com.axnsan.airplanes.util.BasicFontManager.java
com.axnsan.airplanes.util.Detector.java
com.axnsan.airplanes.util.DoubleTapDetector.java
com.axnsan.airplanes.util.DoubleTapListener.java
com.axnsan.airplanes.util.FontManagerInterface.java
com.axnsan.airplanes.util.FontManager.java
com.axnsan.airplanes.util.JavaXmlParser.java
com.axnsan.airplanes.util.Point2D.java
com.axnsan.airplanes.util.RandomizedQueue.java
com.axnsan.airplanes.util.StringManager.java
com.axnsan.airplanes.util.StringXmlParser.java
com.axnsan.airplanes.util.TTFFontManager.java
com.axnsan.airplanes.util.TapDetector.java
com.axnsan.airplanes.util.TapListener.java