/** * Copyright (C) 2018 Christian Pierre MOMON * * 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 */ package org.april.hebdobot.model.stats; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import fr.devinsy.strings.StringList; /** * The Class Integers. */ public class IntegerBoard implements Iterable { private Map integers; private boolean isUptodate; private IntegerStats board; /** * Instantiates a new distribution. */ public IntegerBoard() { this.integers = new HashMap<>(30); this.isUptodate = false; this.board = new IntegerStats(this.integers.size()); } /** * Adds the. * * @param value * the value */ public void add(final Integer value) { if (value != null) { IntegerStat stat = this.integers.get(value); if (stat == null) { stat = new IntegerStat(value); this.integers.put(value, stat); } stat.inc(); } } /** * Gets the. * * @param value * the value * @return the integer stat */ public IntegerStat get(final int value) { IntegerStat result; result = this.integers.get(value); // return result; } /** * Gets the average. * * @return the average */ public double getAverage() { double result; int numerator = 0; int denumerator = 0; for (IntegerStat stat : this.integers.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 (IntegerStat stat : this.integers.values()) { result += stat.getCount(); } // return result; } public int getMaxValue() { int result; if (isEmpty()) { result = 0; } else { result = Integer.MIN_VALUE; for (IntegerStat stat : this.integers.values()) { if (stat.getValue() > result) { result = stat.getValue(); } } } // return result; } /** * Gets the min. * * @return the min */ public int getMinValue() { int result; if (isEmpty()) { result = 0; } else { result = Integer.MAX_VALUE; for (IntegerStat stat : this.integers.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 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).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.integers.size() == 0) { result = true; } else { result = false; } // return result; } /** * Iterator. * * @return the iterator */ @Override public Iterator iterator() { Iterator 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 (IntegerStat 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 (IntegerStat stat : this.integers.values()) { this.board.add(stat); } this.board.sortByValue(); Collections.reverse(this.board); this.isUptodate = false; } } }