301 lines
5.9 KiB
Java
301 lines
5.9 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.bot.stats;
|
|
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
import java.util.Map;
|
|
|
|
import fr.devinsy.strings.StringList;
|
|
|
|
/**
|
|
* The Class LongBoard.
|
|
*/
|
|
public class LongBoard implements Iterable<LongStat>
|
|
{
|
|
private Map<Long, LongStat> longs;
|
|
private boolean isUptodate;
|
|
private LongStats board;
|
|
|
|
/**
|
|
* Instantiates a new distribution.
|
|
*/
|
|
public LongBoard()
|
|
{
|
|
this.longs = new HashMap<>(30);
|
|
this.isUptodate = false;
|
|
this.board = new LongStats(this.longs.size());
|
|
}
|
|
|
|
/**
|
|
* Adds the.
|
|
*
|
|
* @param value
|
|
* the value
|
|
*/
|
|
public void add(final Long value)
|
|
{
|
|
if (value != null)
|
|
{
|
|
LongStat stat = this.longs.get(value);
|
|
if (stat == null)
|
|
{
|
|
stat = new LongStat(value);
|
|
this.longs.put(value, stat);
|
|
}
|
|
|
|
stat.inc();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets the.
|
|
*
|
|
* @param value
|
|
* the value
|
|
* @return the long stat
|
|
*/
|
|
public LongStat get(final long value)
|
|
{
|
|
LongStat result;
|
|
|
|
result = this.longs.get(value);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Gets the average.
|
|
*
|
|
* @return the average
|
|
*/
|
|
public double getAverage()
|
|
{
|
|
double result;
|
|
|
|
int numerator = 0;
|
|
int denumerator = 0;
|
|
for (LongStat stat : this.longs.values())
|
|
{
|
|
numerator += stat.getCount() * stat.getValue();
|
|
denumerator += stat.getCount();
|
|
}
|
|
result = (numerator * 1. / denumerator);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Gets the count sum.
|
|
*
|
|
* @return the count sum
|
|
*/
|
|
public int getCountSum()
|
|
{
|
|
int result;
|
|
|
|
result = 0;
|
|
for (LongStat stat : this.longs.values())
|
|
{
|
|
result += stat.getCount();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
public long getMaxValue()
|
|
{
|
|
long result;
|
|
|
|
if (isEmpty())
|
|
{
|
|
result = 0;
|
|
}
|
|
else
|
|
{
|
|
result = Long.MIN_VALUE;
|
|
for (LongStat stat : this.longs.values())
|
|
{
|
|
if (stat.getValue() > result)
|
|
{
|
|
result = stat.getValue();
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Gets the min.
|
|
*
|
|
* @return the min
|
|
*/
|
|
public long getMinValue()
|
|
{
|
|
long result;
|
|
|
|
if (isEmpty())
|
|
{
|
|
result = 0;
|
|
}
|
|
else
|
|
{
|
|
result = Long.MAX_VALUE;
|
|
for (LongStat stat : this.longs.values())
|
|
{
|
|
if (stat.getValue() < result)
|
|
{
|
|
result = stat.getValue();
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Gets the position of board item.
|
|
*
|
|
* @param search
|
|
* the search
|
|
* @return the position of
|
|
*/
|
|
public Integer getPositionOf(final long search)
|
|
{
|
|
Integer result;
|
|
|
|
update();
|
|
|
|
int index = 0;
|
|
boolean ended = false;
|
|
result = null;
|
|
while (!ended)
|
|
{
|
|
if (index < this.board.size())
|
|
{
|
|
long value = this.board.get(index).getValue();
|
|
|
|
if (value == search)
|
|
{
|
|
result = index + 1;
|
|
ended = true;
|
|
}
|
|
else
|
|
{
|
|
index += 1;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ended = true;
|
|
result = null;
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Checks if is empty.
|
|
*
|
|
* @return true, if is empty
|
|
*/
|
|
public boolean isEmpty()
|
|
{
|
|
boolean result;
|
|
|
|
if (this.longs.size() == 0)
|
|
{
|
|
result = true;
|
|
}
|
|
else
|
|
{
|
|
result = false;
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Iterator.
|
|
*
|
|
* @return the iterator
|
|
*/
|
|
@Override
|
|
public Iterator<LongStat> iterator()
|
|
{
|
|
Iterator<LongStat> result;
|
|
|
|
update();
|
|
result = this.board.iterator();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/* (non-Javadoc)
|
|
* @see java.lang.Object#toString()
|
|
*/
|
|
@Override
|
|
public String toString()
|
|
{
|
|
String result;
|
|
|
|
update();
|
|
|
|
StringList buffer = new StringList();
|
|
for (LongStat stat : this.board)
|
|
{
|
|
buffer.append(String.format("%d (%d)", stat.getValue(), stat.getCount()));
|
|
}
|
|
result = buffer.toStringWithBracket();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Update.
|
|
*/
|
|
public void update()
|
|
{
|
|
if (!this.isUptodate)
|
|
{
|
|
this.board.clear();
|
|
for (LongStat stat : this.longs.values())
|
|
{
|
|
this.board.add(stat);
|
|
}
|
|
this.board.sortByValue();
|
|
Collections.reverse(this.board);
|
|
|
|
this.isUptodate = false;
|
|
}
|
|
}
|
|
}
|