Skip to content

Commit

Permalink
Anonymous SSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
nglthu committed Mar 7, 2024
1 parent 85b0511 commit 09c5e4c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/com/mycompany/app/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import com.mycompany.app.Animal.*;
import com.mycompany.app.Sequence.SequenceA;
import com.mycompany.app.Sequence.SequenceAnonymous;
import com.mycompany.app.Sequence.SelectorA;


Expand Down Expand Up @@ -172,7 +173,7 @@ public static void main(String[] args) {
dog.animalSound();

//Test sequence
SequenceA seq = new SequenceA(10);
SequenceAnonymous seq = new SequenceAnonymous(10);
for(int i=0; i<10; i++){
seq.add(Integer.toString(i));
}
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/com/mycompany/app/Sequence/SequenceAnonymous.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.mycompany.app.Sequence;

public class SequenceAnonymous {
// array of Object
private Object [] obj;
private int next = 0;
private int size;

SequenceAnonymous(){}

public SequenceAnonymous(int size){
obj = new Object[size];
}
//set content
public void add(Object x){
if(next<obj.length) {obj[next] = x; next ++;}
}
//inner class of Subsequence


public SelectorA getSelector(){
return new SelectorA(){
int i = 0;

@Override
public boolean end() {
return i == obj.length;
}

@Override
public Object current() {
return obj[i];
}

@Override
public void next() {
if(i<obj.length) i++;
}
};
}

//anonymous Inner Class

}

0 comments on commit 09c5e4c

Please sign in to comment.