170 lines
3.6 KiB
Java
170 lines
3.6 KiB
Java
/**
|
|
* Copyright (C) 2018 Christian Pierre MOMON <cmomon@april.org>
|
|
*
|
|
* This file is part of (April) Hebdobot.
|
|
*
|
|
* Hebdobot is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* Hebdobot is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with Hebdobot. If not, see <http://www.gnu.org/licenses/>
|
|
*/
|
|
package org.april.hebdobot.model.stats;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.HashSet;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
|
|
import fr.devinsy.strings.StringList;
|
|
|
|
/**
|
|
* The Class Integers.
|
|
*/
|
|
public class SimpleIntegerBoard implements Iterable<Integer>
|
|
{
|
|
private Set<Integer> integers;
|
|
private boolean isUptodate;
|
|
private List<Integer> board;
|
|
|
|
/**
|
|
* Instantiates a new distribution.
|
|
*/
|
|
public SimpleIntegerBoard()
|
|
{
|
|
this.integers = new HashSet<>(30);
|
|
this.isUptodate = false;
|
|
this.board = new ArrayList<>(30);
|
|
}
|
|
|
|
/**
|
|
* Adds the.
|
|
*
|
|
* @param value
|
|
* the value
|
|
* @return true, if successful
|
|
*/
|
|
public boolean add(final Integer value)
|
|
{
|
|
boolean result;
|
|
|
|
if (value == null)
|
|
{
|
|
result = false;
|
|
}
|
|
else
|
|
{
|
|
result = this.integers.add(value);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Gets the position of the board entry.
|
|
*
|
|
* @param search
|
|
* the search
|
|
* @return the position of
|
|
*/
|
|
public Integer getPositionOf(final int search)
|
|
{
|
|
Integer result;
|
|
|
|
update();
|
|
|
|
int index = 0;
|
|
boolean ended = false;
|
|
result = null;
|
|
while (!ended)
|
|
{
|
|
if (index < this.board.size())
|
|
{
|
|
int value = this.board.get(index);
|
|
|
|
if (value == search)
|
|
{
|
|
result = index + 1;
|
|
ended = true;
|
|
}
|
|
else
|
|
{
|
|
index += 1;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ended = true;
|
|
result = null;
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Iterator.
|
|
*
|
|
* @return the iterator
|
|
*/
|
|
@Override
|
|
public Iterator<Integer> iterator()
|
|
{
|
|
Iterator<Integer> result;
|
|
|
|
update();
|
|
result = this.board.iterator();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
public String toString()
|
|
{
|
|
String result;
|
|
|
|
update();
|
|
|
|
StringList buffer = new StringList();
|
|
for (Integer value : this.board)
|
|
{
|
|
buffer.append(value);
|
|
}
|
|
result = buffer.toStringWithBracket();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Update.
|
|
*/
|
|
public void update()
|
|
{
|
|
if (!this.isUptodate)
|
|
{
|
|
this.board.clear();
|
|
for (Integer value : this.integers)
|
|
{
|
|
this.board.add(value);
|
|
}
|
|
Collections.sort(this.board);
|
|
Collections.reverse(this.board);
|
|
|
|
this.isUptodate = false;
|
|
}
|
|
}
|
|
}
|