001: // disorderHeap.java
002: // -----------------------
003: // (C) by Michael Peter Christen; mc@anomic.de
004: // first published on http://www.anomic.de
005: // Frankfurt, Germany, 2004
006: // last major change: 17.05.2004
007: //
008: // This program is free software; you can redistribute it and/or modify
009: // it under the terms of the GNU General Public License as published by
010: // the Free Software Foundation; either version 2 of the License, or
011: // (at your option) any later version.
012: //
013: // This program is distributed in the hope that it will be useful,
014: // but WITHOUT ANY WARRANTY; without even the implied warranty of
015: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: // GNU General Public License for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // along with this program; if not, write to the Free Software
020: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: //
022: // Using this software in any meaning (reading, learning, copying, compiling,
023: // running) means that you agree that the Author(s) is (are) not responsible
024: // for cost, loss of data or any harm that may be caused directly or indirectly
025: // by usage of this softare or this documentation. The usage of this software
026: // is on your own risk. The installation and usage (starting/running) of this
027: // software may allow other people or application to access your computer and
028: // any attached devices and is highly dependent on the configuration of the
029: // software which must be done by the user of the software; the author(s) is
030: // (are) also not responsible for proper configuration and usage of the
031: // software, even if provoked by documentation provided together with
032: // the software.
033: //
034: // Any changes to this file according to the GPL as documented in the file
035: // gpl.txt aside this file in the shipment you received can be done to the
036: // lines that follows this copyright notice here, but changes must not be
037: // done inside the copyright notive above. A re-distribution must contain
038: // the intact and unchanged copyright notice.
039: // Contributions and changes to the program code must be marked as such.
040:
041: package de.anomic.tools;
042:
043: import java.util.LinkedList;
044:
045: public class disorderHeap {
046:
047: LinkedList<String> list;
048:
049: public disorderHeap() {
050: list = new LinkedList<String>();
051: }
052:
053: public disorderHeap(int numbers) {
054: // create a disorder heap with numbers in it
055: // the numbers are 0..numbers-1
056: this ();
057: for (int i = 0; i < numbers; i++)
058: add(Integer.toString(i));
059: }
060:
061: public synchronized void add(String element) {
062: // add one element into the list at an arbitrary position
063: int pos = (int) ((System.currentTimeMillis() / 7) % (list
064: .size() + 1));
065: list.add(pos, element);
066: }
067:
068: public synchronized String remove() {
069: if (list.size() == 0)
070: return null;
071: int pos = (int) ((System.currentTimeMillis() / 13) % list
072: .size());
073: return list.remove(pos);
074: }
075:
076: public synchronized int number() {
077: String n = this .remove();
078: if (n == null)
079: return -1;
080: try {
081: return Integer.parseInt(n);
082: } catch (Exception e) {
083: return -1;
084: }
085: }
086:
087: public synchronized int size() {
088: return list.size();
089: }
090:
091: public static void main(String[] args) {
092: disorderHeap ul = new disorderHeap();
093: for (int i = 0; i < args.length; i++)
094: ul.add(args[i]);
095: for (int i = 0; i < args.length; i++)
096: System.out.print(ul.remove() + " ");
097: System.out.println();
098: }
099:
100: }
|