Skip to content

Instantly share code, notes, and snippets.

@yogonza524
Created July 2, 2018 18:11
Show Gist options
  • Save yogonza524/c2835ffe8ef211cde65d145b15dbff39 to your computer and use it in GitHub Desktop.
Save yogonza524/c2835ffe8ef211cde65d145b15dbff39 to your computer and use it in GitHub Desktop.
private final RecentFileList rfl = new RecentFileList(7);
@Autowired
private List<String> firstInsertBehaviour;
@Test
public void contextLoads() {}
@Test
@Description("Generation test")
public void createRecentFileListTest() {
assertNotNull(firstInsertBehaviour);
System.out.println("First secuence for open files");
System.out.println("Total elements to open: " + firstInsertBehaviour.size());
System.out.println("Recent File List size: " + rfl.size());
firstInsertBehaviour.stream().forEach(file -> {
System.out.println("\n" + file + " opened? " + rfl.add(file));
rfl.showList();
});
}
@Bean
List<String> firstInsertBehaviour() {
List<String> result = new ArrayList<>();
result.add("/home/gonza/myFile.txt");
result.add("/home/gonza/another.txt");
result.add("/home/gonza/myClass.java");
result.add("/home/gonza/thisIsMyExample.txt");
result.add("/home/gonza/gaming.txt");
result.add("/home/gonza/bar.txt");
result.add("/home/gonza/foo.txt");
result.add("/home/gonza/first.txt");
result.add("/home/gonza/example.txt");
result.add("/home/gonza/for.txt");
result.add("/home/gonza/my.txt");
result.add("/home/gonza/TDD.txt");
result.add("/home/gonza/training.txt");
result.add("/home/gonza/voteForYou.txt");
result.add("/home/gonza/happy.txt");
result.add("/home/gonza/myFile.txt");
result.add("/home/gonza/bar.txt");
return result;
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.demo.recentfilelist;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author gonzalo
*/
public class RecentFileList {
private final int maxItems;
private final List<String> files;
public RecentFileList(int maxItems) {
this.maxItems = maxItems;
this.files = new ArrayList<>();
}
public RecentFileList() {
this.maxItems = 15;
this.files = new ArrayList<>();
}
public void showList() {
System.out.println("Recent File list:");
for (int i = 0; i < files.size(); i++) {
System.out.println(i + ". " + files.get(i));
}
}
public boolean add(String fileName) {
if (fileName == null) {
return false;
}
if (fileName.isEmpty()) {
return false;
}
if (this.files.contains(fileName)) {
this.files.remove(fileName);
this.files.add(0,fileName);
return true;
}
if (this.files.size() < this.maxItems) {
this.files.add(0,fileName);
return true;
}
this.files.add(0, fileName);
this.files.remove(this.files.size() - 1);
return true;
}
public int size() {
return this.files.size();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment