-
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.
Commit contains corresponding test case. Signed-off-by: Spaceface16518 <33424649+Spaceface16518@users.noreply.github.com>
- Loading branch information
1 parent
d420c04
commit 7124924
Showing
2 changed files
with
261 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/* | ||
* Copyright 2018 Amrit Rathie | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, | ||
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do | ||
* so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial | ||
* portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | ||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package com.repeater.util; | ||
|
||
import java.io.Flushable; | ||
import java.io.IOException; | ||
import java.io.ObjectOutputStream; | ||
import java.io.OutputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class DynamicBuffer<T> implements Flushable, Backupable { | ||
private static final int minLimit = 10; | ||
private static final int maxLimit = 30; | ||
private static final int limitSpacer = 10; | ||
private OutputStream outputStream; | ||
private List<T> buffer; | ||
// TODO: replace current backup system with separate class | ||
// Also don't make test for the backup system. Or do? Maybe ask an actual | ||
// developer? | ||
private List<List<T>> backup; | ||
private boolean backupEnabled = false; | ||
private int limit; | ||
|
||
public DynamicBuffer() { | ||
buffer = new ArrayList<>(); | ||
} | ||
|
||
DynamicBuffer(OutputStream outputStream) { | ||
this.outputStream = outputStream; | ||
} | ||
|
||
public DynamicBuffer(OutputStream outputStream, int minBufferSize) { | ||
buffer = new ArrayList<>(minBufferSize); | ||
this.outputStream = outputStream; | ||
} | ||
|
||
public synchronized void write(List<? extends T> elements) | ||
throws IOException { | ||
int sizeAfterPush = elements.size() + buffer.size(); | ||
if (sizeAfterPush >= limit && sizeAfterPush >= minLimit) flush(); | ||
respaceBuffer(); | ||
buffer.addAll(elements); | ||
if (limit <= buffer.size()) respaceBuffer(); | ||
} | ||
|
||
/** | ||
* Flush the buffer. | ||
* This empties the buffer into the output stream. | ||
* | ||
* @throws IOException if flush of output stream fails. | ||
*/ | ||
@Override | ||
public void flush() throws IOException { | ||
this.buffer.clear(); | ||
this.outputStream.flush(); | ||
} | ||
|
||
private void respaceBuffer() { | ||
limit = buffer.size() + limitSpacer; | ||
if (limit > maxLimit) { | ||
limit = maxLimit; | ||
} | ||
} | ||
|
||
/** | ||
* This cleans out the buffer settings and resets it to a normal state. | ||
* Use this if the buffer's size or settings get out of hand (This happens sometimes with dynamic objects). | ||
* THIS DOES NOT RESET THE BUFFER'S CONTENTS; IT WILL NOT WIPE THE BUFFER ITSELF! | ||
*/ | ||
public void cleanBuffer() { | ||
limit = minLimit + limitSpacer; | ||
// More resets to come. | ||
} | ||
|
||
@Override | ||
public void cleanBackup() { | ||
this.backup.clear(); | ||
} | ||
|
||
@Override | ||
public void cleanBackup(int... locations) { | ||
for (int location : locations) { | ||
this.backup.remove(location); | ||
} | ||
} | ||
|
||
@Override | ||
public void cleanBackup(int lookBack) { | ||
int last = backup.size() - 1; | ||
int[] locations = new int[lookBack]; | ||
for (int i = 0; i < locations.length; i++) locations[i] = last - i; | ||
this.cleanBackup(locations); | ||
} | ||
|
||
/** | ||
* Backs up the buffer to the backup array. | ||
*/ | ||
@Override | ||
public synchronized void backup() { | ||
ArrayList<T> buf = new ArrayList<>(buffer); // Localize the array for security. | ||
if (this.backupEnabled) backup.add(buf); | ||
} | ||
|
||
@Override | ||
public void backup(int numOfBackups) { | ||
|
||
} | ||
|
||
/** | ||
* Change the output stream. | ||
* This flushes the buffer. | ||
* | ||
* @param outputStream The new output stream. | ||
*/ | ||
public void setOutputStream(ObjectOutputStream outputStream) { | ||
try { | ||
this.flush(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
this.outputStream = outputStream; | ||
} | ||
|
||
/** | ||
* Enables the backup of buffer. | ||
* Backing up the buffer makes it significatly slower, but enables the buffer to be rewinded. | ||
*/ | ||
public void enableBackup() { | ||
backup = new ArrayList<>(); | ||
backupEnabled = true; | ||
} | ||
|
||
/** | ||
* Disables the backup buffer. | ||
* This also clears the backup. | ||
*/ | ||
public void disableBackup() { | ||
backup.clear(); | ||
backupEnabled = false; | ||
} | ||
|
||
public void reset() { | ||
|
||
} | ||
|
||
public void rewind() { | ||
this.buffer.clear(); | ||
this.buffer.addAll(backup.get(backup.size() - 1)); | ||
} | ||
|
||
public void rewind(int lookBack) { | ||
this.buffer.clear(); | ||
this.buffer.addAll(backup.get(lookBack)); | ||
} | ||
} |
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,87 @@ | ||
/* | ||
* Copyright 2018 Amrit Rathie | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, | ||
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do | ||
* so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial | ||
* portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | ||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package com.repeater.util; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.ObjectOutputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
@DisplayName("Dynamic Buffer class tests") | ||
class DynamicBufferTest { | ||
private DynamicBuffer<?> buffer; | ||
private ByteArrayOutputStream out; | ||
private List<String> sample; | ||
|
||
@BeforeEach | ||
void setUp() throws IOException { | ||
buffer = new DynamicBuffer<String>(new ObjectOutputStream(out = new | ||
ByteArrayOutputStream())); | ||
sample = getRand(); | ||
} | ||
|
||
private synchronized static List<String> getRand() { | ||
ArrayList<String> builder = new ArrayList<>(); | ||
Random rand = new Random(); | ||
for (int i = 0; i < 50; i++) | ||
builder.add(String.valueOf(rand.nextDouble() * 15)); | ||
return builder; | ||
} | ||
|
||
@Test | ||
@Disabled | ||
@DisplayName("Write method test") | ||
void writeTest() { | ||
// buffer.write(sample); | ||
// TODO: fix or use the capture part of buffer.write() | ||
} | ||
|
||
@Test | ||
@Disabled | ||
@DisplayName("Flush method test") | ||
void flushTest() { | ||
} | ||
|
||
@Test | ||
@Disabled | ||
@DisplayName("Clean Buffer method test") | ||
void cleanBufferTest() { | ||
} | ||
|
||
@Test | ||
@Disabled | ||
@DisplayName("Set Output Stream method test") | ||
void setOutputStreamTest() { | ||
} | ||
|
||
@Test | ||
@Disabled | ||
@DisplayName("Dynamic test") | ||
void DynamicTest() { | ||
} | ||
} |
7124924
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commit contains soon to be deprecated technology.