Skip to content

cgu4 dcb31 #3

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# example_bins
First example code for refactoring

Adding using vim to test**
=======
This line was added on GitHub

THis line was added before Eclipse

This line was added with Eclipse

16 changes: 9 additions & 7 deletions src/Bins.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

Expand All @@ -9,9 +10,9 @@
public class Bins {
public static final String DATA_FILE = "example.txt";
// all possible algorithms to compare --- add new instances here!
private static WorstFitAlgorithm algortihmsToCompare[] = {
new WorstFitAlgorithm(),
new WorstFitDecreasingAlgorithm()
private static ITransformList algortihmsToCompare[] = {
(List<Integer> l) -> Collections.sort(l, Collections.reverseOrder()),
(List<Integer> l) -> l=l
};


Expand Down Expand Up @@ -49,9 +50,10 @@ public static void main (String args[]) {
Scanner input = new Scanner(Bins.class.getClassLoader().getResourceAsStream(DATA_FILE));
List<Integer> data = b.readData(input);
System.out.println("total size = " + b.getTotal(data) / 1000000.0 + "GB");

for (WorstFitAlgorithm al : algortihmsToCompare) {
al.fitDisksAndPrint(data);
WorstFitAlgorithm wf = new WorstFitAlgorithm();

for (ITransformList al : algortihmsToCompare) {
wf.fitDisksAndPrint(data, al);
}
}
}
}
8 changes: 8 additions & 0 deletions src/ITransformList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import java.util.List;

@FunctionalInterface
public interface ITransformList {

public void fitDisksAndPrintLambda(List<Integer> l);

}
4 changes: 3 additions & 1 deletion src/WorstFitAlgorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public WorstFitAlgorithm (String description) {
*
* @param data collection of files to be allocated to disks
*/
public void fitDisksAndPrint (List<Integer> data) {
public void fitDisksAndPrint (List<Integer> data, ITransformList lambda) {
List<Integer> copy = new ArrayList<>(data);
lambda.fitDisksAndPrintLambda(data);
organizeData(copy);
Collection<Disk> disks = addFiles(copy);
printResults(disks, myDescription);
Expand Down Expand Up @@ -74,4 +75,5 @@ private void printResults (Collection<Disk> disks, String description) {
System.out.println(d);
}
}

}