Skip to content

Instantly share code, notes, and snippets.

Created March 2, 2015 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/97c810c45b54d0dd3841 to your computer and use it in GitHub Desktop.
Save anonymous/97c810c45b54d0dd3841 to your computer and use it in GitHub Desktop.
package javaapplication8;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
public class InMemoryTrollRepository {
private final List<Troll> trolls = new LinkedList<>();
public InMemoryTrollRepository() {
Troll perceptroll = new Troll();
perceptroll.setId(1L);
perceptroll.setNick("perceptroll");
trolls.add(perceptroll);
Troll kyt = new Troll();
kyt.setId(2L);
kyt.setNick("kyt");
trolls.add(kyt);
}
public List<Troll> list() {
return Collections.unmodifiableList(trolls);
}
public int count() {
return trolls.size();
}
public Optional<Troll> findById(Long id) {
return trolls
.stream()
.filter((troll) -> troll.getId().equals(id))
.findFirst();
}
}
package javaapplication8;
public class Troll {
private Long id;
private String nick;
private boolean hungry;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
public boolean isHungry() {
return hungry;
}
public void setHungry(boolean hungry) {
this.hungry = hungry;
}
}
package javaapplication8;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import static javax.swing.BoxLayout.LINE_AXIS;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class TrollDetailForm extends JFrame {
private final InMemoryTrollRepository trollRepository = new InMemoryTrollRepository();
private final JLabel trollLabel = new JLabel();
public TrollDetailForm() {
setLayout(new BoxLayout(getContentPane(), LINE_AXIS));
initializeModel();
trollLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
add(trollLabel);
setSize(800, 600);
pack();
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void initializeModel() {
Troll troll = trollRepository.findById(2L).get();
StringBuilder trollStatus = new StringBuilder("Troll ");
trollStatus.append(troll.getNick());
if(troll.isHungry()) {
trollStatus.append(" needs food");
} else {
trollStatus.append(" needs food");
}
trollLabel.setText(trollStatus.toString());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new TrollDetailForm());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment