forked from CyberBiology/CyberBiology
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: добавление истории выполненных генов бота
- Loading branch information
Showing
4 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package ru.cyberbiology; | ||
|
||
/** | ||
* Класс представляет собой список выполненных генов в виде очереди. | ||
* | ||
* <p>В отличие от классической очереди, данная очередь не позволяет | ||
* извлекать или получать элементы, их можно только добавлять. Эта очередь | ||
* имеет фиксированный размер. При заполнении очереди, вновь добавляемые | ||
* элементы переписывают самые старые. | ||
* Все элементы очереди можно получить в виде массива с помощью <code>toArray()</code>. | ||
* | ||
* @author Sergey Sokolov ([email protected]) | ||
*/ | ||
class GeneQueue { | ||
|
||
private final int size; | ||
private final int[] data; | ||
private int tail; | ||
private int count = 0; | ||
|
||
/** | ||
* Создает объект очереди. | ||
* | ||
* @param size размер очереди - максимальное количество сохраняемых элементов | ||
*/ | ||
public GeneQueue(int size) { | ||
this.size = size; | ||
data = new int[size]; | ||
} | ||
|
||
/** | ||
* Проверяет пуста ли очередь. | ||
* | ||
* @return <code>true</code> если очередь пуста; | ||
* <code>false</code> в противном случае | ||
*/ | ||
public boolean isEmpty() { | ||
return count == 0; | ||
} | ||
|
||
/** | ||
* Возвращает количество элементов в очереди. | ||
* | ||
* @return количество элементов в очереди | ||
*/ | ||
public int count() { | ||
return count; | ||
} | ||
|
||
/** | ||
* Добавляет элемент в очередь. | ||
* | ||
* @param i добавляемый элемент | ||
*/ | ||
public void add(int i) { | ||
if (++tail == size) { | ||
tail = 0; | ||
} | ||
data[tail] = i; | ||
if (count < size) { | ||
count++; | ||
} | ||
} | ||
|
||
/** | ||
* Возвращает представление очереди в виде массива. | ||
* | ||
* <p>Массив будет иметь размер, соответствующий количеству элементов. | ||
* Элементы будут расположены в порядке от самого нового до самого старого. | ||
* | ||
* @return упорядоченный массив очереди | ||
*/ | ||
public int[] toArray() { | ||
if (isEmpty()) { | ||
return null; | ||
} | ||
int[] result = new int[count]; | ||
int j = tail + 1; | ||
for (int i = 0; i < count; i++) { | ||
if (--j < 0) { | ||
j = size - 1; | ||
} | ||
result[i] = data[j]; | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
if (isEmpty()) { | ||
return "[]"; | ||
} | ||
int[] arr = toArray(); | ||
String result = "[" + arr[0]; | ||
for (int i = 1; i < arr.length; i++) { | ||
result += "," + arr[i]; | ||
} | ||
return result += "]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package ru.cyberbiology; | ||
|
||
import org.junit.After; | ||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
import org.junit.Before; | ||
|
||
/** | ||
* | ||
* @author Sergey Sokolov ([email protected]) | ||
*/ | ||
public class GeneQueueTest { | ||
|
||
private final int size = 5; | ||
private GeneQueue queue; | ||
|
||
@Before | ||
public void setUp() { | ||
queue = new GeneQueue(size); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
queue = null; | ||
} | ||
|
||
@Test | ||
public void newQueueIsEmpty() { | ||
assertTrue(queue.isEmpty()); | ||
assertEquals(0, queue.count()); | ||
} | ||
|
||
@Test | ||
public void emptyQueueToArrayReturnsNull() { | ||
assertNull(queue.toArray()); | ||
} | ||
|
||
@Test | ||
public void queueElementsCountEqualsAddedElementsCount() { | ||
add(3); | ||
assertEquals(3, queue.count()); | ||
} | ||
|
||
@Test | ||
public void elementsCountDoesNotExceedQueueSize() { | ||
add(6); | ||
assertEquals(size, queue.count()); | ||
} | ||
|
||
@Test | ||
public void addingInFullQueueOverwritesTheOldestElement() { | ||
add(5); | ||
queue.add(25); | ||
assertEquals(25, (queue.toArray())[0]); | ||
queue.add(30); | ||
assertEquals(30, (queue.toArray())[0]); | ||
} | ||
|
||
private void add(int count) { | ||
for (int i = 0; i < count; i++) { | ||
queue.add(i+1); | ||
} | ||
} | ||
} |