Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor simplify api #106

Merged
merged 2 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@
/core/nuts/.jpeek/
/core/nuts-runtime/.jpeek/
/core/.jpeek/
/.gradle/
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ public static Class[] findClassHierarchy(Class clazz, Class baseType) {
return result.toArray(new Class[result.size()]);
}

public Set<V> keySet() {
return new HashSet(values.keySet());
}

public Collection<V> values() {
return values.values();
}

public V put(Class classKey, V value) {
cachedValues.clear();
return values.put(classKey, value);
Expand Down Expand Up @@ -208,4 +216,7 @@ public void clear() {
cachedHierarchy.clear();
}

public int size() {
return values.size();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package net.thevpc.nuts.runtime.bundles.collections;

import java.util.Arrays;

public class EvictingIntQueue {
private int from=0;
private int len;
private int[] values;

public EvictingIntQueue(int max) {
this.values=new int[max];
}

public void clear() {
from=0;
len=0;
}

public int size() {
return len;
}

public int get(int pos) {
if(pos>=0 && pos<len) {
int i=(from+pos)%values.length;
return values[i];
}
throw new IllegalArgumentException("invalid index "+pos);
}

public void add(int t) {
int pos=(from+len)%values.length;
values[pos]=t;
if(len<values.length){
len++;
}else{
from=(from+1)%values.length;
}
}

public String dump() {
StringBuilder sb=new StringBuilder();
for (int i = 0; i < size(); i++) {
sb.append(get(i));
}
return "EvictingIntQueue{" +
"from=" + from +
", len=" + len +
", raw=" + Arrays.toString(values) +
", values='" + sb +"'"+
'}';
}

@Override
public String toString() {
StringBuilder sb=new StringBuilder("{");
for (int i = 0; i < size(); i++) {
if(i>0){
sb.append(",");
}
sb.append(get(i));
}
sb.append("}");
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,69 @@
package net.thevpc.nuts.runtime.bundles.collections;

import java.util.LinkedList;
import java.util.AbstractList;
import java.util.Arrays;

public class EvictingQueue<T> {
int max;
LinkedList<T> values = new LinkedList<>();
public class EvictingQueue<T> extends AbstractList<T>{
private int from=0;
private int len;
private Object[] values;

public EvictingQueue(int max) {
this.max = max;
this.values=new Object[max];
}

public void clear() {
values.clear();
from=0;
len=0;
}

public int size() {
return values.size();
return len;
}

public T get(int pos) {
return values.get(pos);
if(pos>=0 && pos<len) {
int i=(from+pos)%values.length;
return (T)values[i];
}
throw new IllegalArgumentException("invalid index "+pos);
}

public void add(T t) {
if (values.size() >= max) {
values.removeFirst();

public boolean add(T t) {
int pos=(from+len)%values.length;
values[pos]=t;
if(len<values.length){
len++;
}else{
from=(from+1)%values.length;
}
values.add(t);
return true;
}

public String dump() {
StringBuilder sb=new StringBuilder();
for (int i = 0; i < size(); i++) {
sb.append(get(i));
}
return "EvictingQueue{" +
"from=" + from +
", len=" + len +
", raw=" + Arrays.toString(values) +
", values='" + sb +"'"+
'}';
}

@Override
public String toString() {
StringBuilder sb=new StringBuilder("{");
for (int i = 0; i < size(); i++) {
if(i>0){
sb.append(",");
}
sb.append(get(i));
}
sb.append("}");
return sb.toString();
}
}
Loading