hebdobot/src/org/april/hebdobot/bot/stats/ReviewDatas.java

328 lines
6.3 KiB
Java

/**
* Copyright (C) 2017-2019 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.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import org.april.hebdobot.bot.stats.ReviewDataComparator.Sorting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class Stats.
*/
public class ReviewDatas extends ArrayList<ReviewData>
{
private static final long serialVersionUID = 326880908257489774L;
private static final Logger logger = LoggerFactory.getLogger(ReviewDatas.class);
/**
* Instantiates a new ReviewDatas.
*/
public ReviewDatas()
{
super();
}
/**
* Instantiates a new review datas.
*
* @param initialCapacity
* the initial capacity
*/
public ReviewDatas(final int initialCapacity)
{
super(initialCapacity);
}
/**
* Instantiates a new review datas.
*
* @param source
* the source
*/
public ReviewDatas(final ReviewDatas source)
{
super(source);
}
/**
* Clean.
*
* @return the int
*/
public int clean()
{
int result;
result = 0;
Iterator<ReviewData> iterator = iterator();
while (iterator.hasNext())
{
if (iterator.next().getUserCount() < 2)
{
iterator.remove();
result += 1;
}
}
logger.debug("Cleaned review data: {}", result);
//
return result;
}
/**
* Count by year.
*
* @param year
* the year
* @return the long
*/
public long countByYear(final long year)
{
long result;
result = getByYear(year).size();
//
return result;
}
/**
* Gets the by year.
*
* @param year
* the year
* @return the by year
*/
public ReviewDatas getByYear(final long year)
{
ReviewDatas result;
result = new ReviewDatas();
for (ReviewData data : this)
{
if (data.getDate().getYear() == year)
{
result.add(data);
}
}
//
return result;
}
/**
* Gets the last by index.
*
* @return the last by index
*/
public ReviewData getLastByIndex()
{
ReviewData result;
if (isEmpty())
{
result = null;
}
else
{
result = this.get(size() - 1);
}
//
return result;
}
/**
* Gets the last by max user count.
*
* @return the last by max user count
*/
public ReviewData getLastByMaxUserCount()
{
ReviewData result;
if (isEmpty())
{
result = null;
}
else
{
int max = Integer.MIN_VALUE;
result = null;
for (ReviewData data : this)
{
if (data.getUserCount() > max)
{
max = data.getUserCount();
result = data;
}
}
}
//
return result;
}
/**
* Gets the max user count.
*
* @return the max user count
*/
public int getMaxUserCount()
{
int result;
if (isEmpty())
{
result = 0;
}
else
{
result = Integer.MIN_VALUE;
for (ReviewData data : this)
{
if (data.getUserCount() > result)
{
result = data.getUserCount();
}
}
}
//
return result;
}
/**
* Gets the min user count.
*
* @return the min user count
*/
public int getMinUserCount()
{
int result;
if (isEmpty())
{
result = 0;
}
else
{
result = Integer.MAX_VALUE;
for (ReviewData data : this)
{
if (data.getUserCount() < result)
{
result = data.getUserCount();
}
}
}
//
return result;
}
/**
* Gets the previous last by index.
*
* @return the previous last by index
*/
public ReviewData getPreviousLastByIndex()
{
ReviewData result;
if ((isEmpty()) || (size() == 1))
{
result = null;
}
else
{
result = this.get(size() - 2);
}
//
return result;
}
/**
* Removes the first.
*
* @return the review datas
*/
public ReviewDatas removeFirst()
{
if (!isEmpty())
{
remove(0);
}
//
return this;
}
/**
* Removes the last.
*/
public ReviewDatas removeLast()
{
if (!isEmpty())
{
remove(size() - 1);
}
//
return this;
}
/**
* Reverse.
*/
public void reverse()
{
Collections.reverse(this);
}
/**
* Sort by date.
*/
public void sortByDate()
{
Collections.sort(this, new ReviewDataComparator(Sorting.DATE));
}
/**
* Sort by duration.
*/
public void sortByDuration()
{
Collections.sort(this, new ReviewDataComparator(Sorting.DURATION));
}
/**
* Sort by user count.
*/
public void sortByUserCount()
{
Collections.sort(this, new ReviewDataComparator(Sorting.USERCOUNT));
}
}