id
stringlengths
36
36
text
stringlengths
1
1.25M
54a08e79-f74e-4eaa-a6d9-d896f913d660
private boolean cancel(CommandSender sender, String[] args) { ban = null; sender.sendMessage(ChatColor.YELLOW + "Cancelled."); return true; }
8de1dd5c-7b38-46c3-9ec5-a7211d7c5a7e
public FigAdminPlayerListener(FigAdmin instance) { this.plugin = instance; }
bf19a2ea-d947-48ff-b3c6-de4e20edd5a8
@EventHandler(priority = EventPriority.HIGH) public void onPlayerLogin(PlayerLoginEvent event) { Player player = event.getPlayer(); for (int i = 0; i < plugin.bannedPlayers.size(); i++) { EditBan e = plugin.bannedPlayers.get(i); if (e.name.equals(player.getName().toLowerCase())) { long tempTime = e.endTime; boolean tempban = false; if (tempTime > 0) { // Player is banned. Check to see if they are still banned // if it's a tempban long now = System.currentTimeMillis()/1000; long diff = tempTime - now; if (diff <= 0) { plugin.bannedPlayers.remove(i); return; } tempban = true; } Date date = new Date(); date.setTime(tempTime * 1000); String kickerMsg = null; if (tempban) { kickerMsg= plugin.formatMessage(plugin.getConfig().getString("messages.LoginTempban")); kickerMsg = kickerMsg.replaceAll("%time%", date.toString()); kickerMsg = kickerMsg.replaceAll("%reason%", e.reason); } else if (e.type == EditBan.BAN) { // make sure it isn't an ipban kickerMsg = plugin.formatMessage(plugin.getConfig().getString("messages.LoginBan")); kickerMsg = kickerMsg.replaceAll("%time%", date.toString()); kickerMsg = kickerMsg.replaceAll("%reason%", e.reason); } if (kickerMsg != null) { event.disallow(PlayerLoginEvent.Result.KICK_OTHER, kickerMsg); return; } } } }
7bf5be2e-a36b-49a6-9269-35bc4ec8ce04
public boolean initialize(FigAdmin plugin) { this.plugin = plugin; banlist = new File("plugins/FigAdmin/banlist.txt"); if (!banlist.exists()) { try { banlist.createNewFile(); } catch (IOException e) { e.printStackTrace(); return false; } } return true; }
78abfa51-a6d5-4aa0-ba6e-d8b8092a3744
@Override public boolean removeFromBanlist(String player) { player = player.toLowerCase(); try { File tempFile = new File(banlist.getAbsolutePath() + ".tmp"); BufferedReader br = new BufferedReader(new FileReader(banlist)); PrintWriter pw = new PrintWriter(new FileWriter(tempFile)); String line = null; // Loops through the temporary file and deletes the player while ((line = br.readLine()) != null) { boolean match = false; if (!line.startsWith("#")) { String[] data = line.split("\\|"); if (data.length > 1) { match = data[0].equals(player) && Integer.parseInt(data[1]) != 2; } } if (!match) { pw.println(line); pw.flush(); } } pw.close(); br.close(); // Let's delete the old banlist.txt and change the name of our // temporary list! banlist.delete(); tempFile.renameTo(banlist); return true; } catch (Exception ex) { ex.printStackTrace(); return false; } }
6d6d88cd-cd24-40f7-a185-baf83463a49f
@Override public void addPlayer(EditBan b) { b.id = id++; try { BufferedWriter writer = new BufferedWriter(new FileWriter(banlist, true)); writer.write(b.toString()); writer.newLine(); writer.close(); } catch (IOException e) { FigAdmin.log.log(Level.SEVERE, "FigAdmin: Couldn't write to banlist.txt"); } }
344a8b9d-8d9a-406d-a8a3-27539c6caa43
@Override public ArrayList<EditBan> getBannedPlayers() { id = 0; if (!banlist.exists()) { try { banlist.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } ArrayList<EditBan> list = new ArrayList<EditBan>(); try { BufferedReader in = new BufferedReader(new FileReader(banlist)); String data = null; while ((data = in.readLine()) != null) { // Checking for blank lines if (!data.startsWith("#")) { if (data.length() > 0) { EditBan e = EditBan.loadBan(data); if (e != null && e.type != 2) { list.add(e); id = Math.max(e.id, id); } } } } id++; in.close(); } catch (IOException e) { e.printStackTrace(); } return list; }
ec106bc2-512b-4d46-a191-683934bcfb7d
@Override public void updateAddress(String player, String ip) { player = player.toLowerCase(); try { File tempFile = new File(banlist.getAbsolutePath() + ".temp"); BufferedReader br = new BufferedReader(new FileReader(banlist)); PrintWriter pw = new PrintWriter(new FileWriter(tempFile)); String line = null; // Loops through the temporary file and deletes the player while ((line = br.readLine()) != null) { if (!line.startsWith("#")) { String[] data = line.split("\\|"); if (data.length > 7) { if (data[0].equals(player)) { data[4] = ip; line = EditBan.loadBan(data).toString(); } else if (data[4].equals(ip)) { data[0] = player; line = EditBan.loadBan(data).toString(); } } } pw.println(line); pw.flush(); } pw.close(); br.close(); // Let's delete the old banlist.txt and change the name of our // temporary list! banlist.delete(); tempFile.renameTo(banlist); } catch (Exception ex) { ex.printStackTrace(); } }
f10bf61b-e82f-465f-94d9-9116fb1f6ee4
@Override protected EditBan loadFullRecord(String pName) { return loadFullRecord(pName, 0); }
ade393a8-53d5-42e3-b815-64c681d06ae8
@Override protected EditBan loadFullRecord(int id) { return loadFullRecord(null, id); }
cfe08950-9a11-4e38-b790-54726acf44a1
private EditBan loadFullRecord(String player, int id) { if (player != null) player = player.toLowerCase(); EditBan ban = null; try { BufferedReader br = new BufferedReader(new FileReader(banlist)); String line = null; // Loops through the temporary file and deletes the player while ((line = br.readLine()) != null) { if (line.startsWith("#")) continue; String[] data = line.split("\\|"); if (data.length > 7) { if (player != null && data[0].equals(player)) { ban = EditBan.loadBan(data); break; } else if (Integer.parseInt(data[1]) == id) { ban = EditBan.loadBan(data); break; } } } br.close(); } catch (Exception ex) { ex.printStackTrace(); } return ban; }
bb738aec-5e1a-4662-a8dd-3d3593189406
@Override public boolean saveFullRecord(EditBan ban) { try { File tempFile = new File(banlist.getAbsolutePath() + ".tmp"); BufferedReader br = new BufferedReader(new FileReader(banlist)); PrintWriter pw = new PrintWriter(new FileWriter(tempFile)); boolean written = false; String line = null; // Loops through the temporary file and deletes the player while ((line = br.readLine()) != null) { if (line.startsWith("#")) continue; if (!written) { String[] data = line.split("\\|"); if (data.length > 7) { if (Integer.parseInt(data[1]) == ban.id) { line = ban.toString(); written = true; } } } pw.println(line); pw.flush(); } br.close(); pw.close(); banlist.delete(); tempFile.renameTo(banlist); return written; } catch (Exception ex) { ex.printStackTrace(); } return false; }
3d600b6a-667a-449e-b755-6b168e4b1876
@Override public ArrayList<EditBan> listRecords(String name, boolean exact) { name = name.toLowerCase(); ArrayList<EditBan> bans = new ArrayList<EditBan>(); try { BufferedReader br = new BufferedReader(new FileReader(banlist)); String line = null; // Loops through the temporary file and deletes the player while ((line = br.readLine()) != null) { if (line.startsWith("#")) continue; String[] data = line.split("\\|"); if (data.length > 7) { if ((exact && data[0].equalsIgnoreCase(name)) || (!exact && data[0].contains(name))) { EditBan ban = EditBan.loadBan(data); if (ban != null) { bans.add(ban); if (bans.size() > 9) { break; } } } } } br.close(); } catch (Exception ex) { ex.printStackTrace(); } return bans; }
7cd59b7f-582a-4d30-a10b-dd504763d092
@Override protected boolean deleteFullRecord(int id) { try { File tempFile = new File(banlist.getAbsolutePath() + ".tmp"); BufferedReader br = new BufferedReader(new FileReader(banlist)); PrintWriter pw = new PrintWriter(new FileWriter(tempFile)); String line = null; // Loops through the temporary file and deletes the player while ((line = br.readLine()) != null) { boolean match = false; if (!line.startsWith("#")) { String[] data = line.split("\\|"); if (data.length > 1) { match = Integer.parseInt(data[1]) == id; } } if (!match) { pw.println(line); pw.flush(); } } pw.close(); br.close(); // Let's delete the old banlist.txt and change the name of our // temporary list! banlist.delete(); tempFile.renameTo(banlist); return true; } catch (Exception ex) { ex.printStackTrace(); return false; } }
1cafe47d-e6c7-4120-ba36-19af0b4e4856
@Override public int getWarnCount(String player) { ArrayList<EditBan> records = listRecords(player, true); int warns = 0; for (EditBan e : records) { if (e.type == EditBan.WARN) warns++; } return warns; }
09150408-8505-4d28-b2b8-7d37f02921ed
@Override public int clearWarnings(String player) { int warns = 0; try { File tempFile = new File(banlist.getAbsolutePath() + ".tmp"); BufferedReader br = new BufferedReader(new FileReader(banlist)); PrintWriter pw = new PrintWriter(new FileWriter(tempFile)); String line = null; // Loops through the temporary file and deletes the player's // warnings while ((line = br.readLine()) != null) { if (line.startsWith("#")) continue; boolean skip = false; EditBan b = EditBan.loadBan(line); if (b != null) { if (b.name.equalsIgnoreCase(player) && b.type == EditBan.WARN) { skip = true; warns++; } } if (!skip) pw.println(line); skip = false; pw.flush(); } br.close(); pw.close(); banlist.delete(); tempFile.renameTo(banlist); } catch (Exception ex) { ex.printStackTrace(); } return warns; }
38280a7b-c51c-443a-ba7d-033ea3bb8748
EditBan(int id, String name, String reason, String admin, long time, long endTime, int type, String IP) { this.id = id; this.name = name.toLowerCase(); this.reason = reason; this.admin = admin; this.time = time; this.endTime = endTime; this.type = type; this.IP = IP; }
339461fe-6375-440c-b241-d3bc037e7914
EditBan(String name, String reason, String admin, int type) { this.id = 0; this.name = name.toLowerCase(); this.reason = reason; this.admin = admin; this.time = System.currentTimeMillis(); this.endTime = 0; this.type = type; this.IP = null; }
6b119f31-f512-48b1-8db9-b53e0ced5b89
EditBan(String name, String reason, String admin, String IP, int type) { this.id = 0; this.name = name.toLowerCase(); this.reason = reason; this.admin = admin; this.time = System.currentTimeMillis(); this.endTime = 0; this.type = type; this.IP = IP; }
d93ea6f1-b64d-41f3-822b-3e94ffcfde06
EditBan(String name, String reason, String admin, long endTime, int type) { this.id = 0; this.name = name.toLowerCase(); this.reason = reason; this.admin = admin; this.time = System.currentTimeMillis(); this.endTime = endTime; this.type = type; this.IP = null; }
0f673486-3f35-408b-a03b-945abbc670ca
private EditBan() { }
f9ecac37-1131-4e0a-88ac-8604e8011dd0
public static EditBan loadBan(String data) { String[] d = data.split("\\|"); return loadBan(d); }
58efaa24-e9ae-4a7c-a6f4-05bc5e33bb4c
public static EditBan loadBan(String[] d) { if (d.length < 7) { return null; } EditBan e = new EditBan(); e.name = d[0].toLowerCase(); e.id = Integer.parseInt(d[1]); e.reason = d[2]; e.admin = d[3]; e.IP = (d[4].equals("null")) ? null : d[4]; e.time = Long.parseLong(d[5]); e.endTime = Long.parseLong(d[6]); e.type = Integer.parseInt(d[7]); return e; }
f7fedf42-a64c-4025-8188-7fc295e3932f
public String toString() { StringBuffer s = new StringBuffer(id); s.append(name); s.append("|"); s.append(id); s.append("|"); s.append(reason); s.append("|"); s.append(admin); s.append("|"); s.append(IP); s.append("|"); s.append(time); s.append("|"); s.append(endTime); s.append("|"); s.append(type); return s.toString(); }
ac61f564-7f76-41df-b08b-714f29bc66db
public boolean equals(Object object) { if (object instanceof String) { return ((String)object).toLowerCase().equals(this.name); } else if (object instanceof EditBan) { EditBan o = (EditBan) object; return o.name.equals(this.name) && o.admin.equals(this.admin) && o.reason.equals(this.reason) && o.IP.equals(this.IP) && o.time == this.time && o.endTime == this.endTime && o.type == this.type; } return false; }
8cd27a07-3ae4-422f-934e-42507d4dd13a
public abstract boolean initialize(FigAdmin plugin);
87d08547-bb88-4fb2-a0f5-1fc9e9aa0ee4
public abstract boolean removeFromBanlist(String player);
7d143e26-fbe0-4055-8bca-997ca3bccc80
public abstract void addPlayer(EditBan e);
f8c9efdb-d42e-4262-86df-999c8786dfd7
public abstract ArrayList<EditBan> getBannedPlayers();
889be6a2-7ef2-4655-be4e-b815a90e7cd5
public abstract void updateAddress(String p, String ip);
e076297b-ec8c-47b8-a413-41e1d00fdae2
protected abstract EditBan loadFullRecord(String pName);
137b649d-78af-47f5-93cd-5df91c4f7183
protected abstract EditBan loadFullRecord(int id);
a5ef2c8e-4f69-4793-bbff-53777f9ebde3
protected abstract boolean deleteFullRecord(int id);
8cc8c93c-b778-4f99-a714-313ca32b0498
public abstract ArrayList<EditBan> listRecords(String name, boolean exact);
7c602b2e-6c52-4e5c-b488-448e513d97c3
public abstract boolean saveFullRecord(EditBan ban);
23566244-6fb4-4b46-9208-3a87ba1496ec
public abstract int getWarnCount(String player);
af5655b9-46d3-4a5a-8fe8-538c0fadba4a
public abstract int clearWarnings(String player);
8e9fd19c-193f-43ea-893f-4c1c643989aa
public TTree() { nodes = new ArrayList<TNode>(); }
d2105df0-84e4-4c1a-bb7b-0b59917e89f4
public TTree(List<TNode> input) { this.nodes = input; }
460a5508-c335-4895-86c4-bf6a0401497c
public List<TNode> getNodes() { return nodes; }
bd021084-6e28-47ea-ae07-e6fc8cfba4e7
public void setNodes(List<TNode> nodes) { this.nodes = nodes; }
0c51d121-4dd6-4bf4-97e7-6b1eff2ba046
public void addNode(TNode toAdd) { if (toAdd != null) { List<String> titles = new ArrayList<String>(); TNode exists = null; for(TNode n : nodes) { titles.add(n.getTitle()); if(n.getTitle().equals(toAdd.getTitle())) { exists = n; } } if(!titles.contains(toAdd.getTitle())) { nodes.add(toAdd); } else { List<TConnection> connects = toAdd.getConnections(); for(TConnection con : connects) { exists.addConnection(con); } } } }
2aed54ad-0639-4995-a5b0-06aa4b58c488
public void removeNode(TNode toRemove) { if(toRemove != null) for(TNode n : nodes) if(n.equals(toRemove)) nodes.remove(n); }
c33570db-1873-405d-aebe-decb44e352c6
public void addNodes(List<TNode> toAdd) { if (toAdd != null) { for(TNode n : toAdd) if(!hasNode(n)) nodes.add(n); } }
400824c7-5d84-4987-93d2-7177f80e8b6d
public boolean hasNode(String in) { boolean hasNode = false; for(TNode node : this.nodes) { if(node.getTitle().equalsIgnoreCase(in)) { hasNode = true; break; } } return hasNode; }
a8c50c88-0e5c-47a9-b3c7-70fac5986ee3
public boolean hasNode(TNode in) { boolean hasNode = false; for (TNode node : this.nodes) { if (node.equals(in)) { hasNode = true; break; } } return hasNode; }
ddf1998e-4768-4dd6-a90d-7b803b399d66
@Override public String toString() { String ret = ""; if (nodes != null && nodes.size() > 0) { StringBuilder sb = new StringBuilder(); List<TConnection> printed = new ArrayList<TConnection>(); for (TNode n : nodes) { for (TConnection c : n.getConnections()) { if(!printed.contains(c)) printed.add(c); } } for(TConnection c : printed) { sb.append(c + "\n"); } ret = sb.toString(); } else ret = "Empty Tree"; return ret; }
cbb31880-8d73-481a-8040-968f280f35fd
public TNode(String title) { this.title = title; connections = new ArrayList<TConnection>(); }
4bc612ee-8b89-4560-88e6-3f092bf15e17
public List<TConnection> getConnections() { return connections; }
29e5473a-0739-482f-89a7-5c0cf42ea161
public String getTitle() { return title; }
ef4fcdbe-ef7b-4d06-8981-16d8fa4d54d5
public void setTitle(String title) { this.title = title; }
c0718740-5e36-408e-96a3-b7eb64865d7a
public void setConnections(List<TConnection> connections) { this.connections = connections; }
275bd411-2dcb-49b1-a40f-ab2e77384a97
public void addConnection(TConnection t) { if (t != null) connections.add(t); }
6285294a-0dde-463c-8dfe-9a4916d40982
private void trimConnections() { // List<String> toEndPoints = new ArrayList<String>(); // List<String> dups = new ArrayList<String>(); // for(TConnection t : connections) { // if(toEndPoints.contains(t.getTNodeTwo()) && // !dups.contains(t.getTNodeTwo())) { // dups.add(t.getTNodeTwo()); // } else { // toEndPoints.add(t.getTNodeTwo()); // } // } // // for(String s : dups) { // List<TConnection> toTrim = new ArrayList<TConnection>(); // for(TConnection connect : connections) { // if(connect.getTNodeTwo().equals(s)) // toTrim.add(connect); // } // if(toTrim.size() > 0) { // int min = toTrim.get(0).getWeight(); // for(TConnection connect : toTrim) { // if(connect.getWeight() < min) // min = connect.getWeight(); // } // } // } }
c5d09519-05c9-4bb1-86a0-7e4e07aec731
@Override public String toString() { return this.getTitle(); }
f0f1e8db-4e4e-45a6-a381-d57a3cb7e646
public boolean equals(TNode o) { return o.getTitle().equals(this.title); }
283287e7-f4b0-45a8-accc-c5ab9d7cbed5
public static TNode copyNode(TNode in) { TNode newNode = new TNode(in.getTitle()); List<TConnection> connects = newNode.getConnections(); for(TConnection c : connects) { newNode.addConnection(c); } return newNode; }
e042fb71-151b-47c4-9595-2b3ca03e2d03
public TConnection() {}
f84c9019-8338-413d-9569-7070ccefaf53
public TConnection(TNode one, String two, int weight) { this.TNodeOne = one; this.TNodeTwoTitle = two; this.weight = weight; }
d27ca397-d206-4ce5-9ab8-30f8328c1332
public int getWeight() { return weight; }
e63a2026-e2f7-40ad-8b59-fd8e06a7f9be
public void setWeight(int weight) { this.weight = weight; }
64b782b7-0d45-48f0-aad0-bcaec9545ae5
public TNode getTNodeOne() { return TNodeOne; }
f63f5446-e12d-4846-b8f3-6cedfde6bbf1
public void setTNodeOne(TNode tNodeOne) { TNodeOne = tNodeOne; }
4ed97b79-2c4d-49c4-947c-13dba26e9451
public String getTNodeTwo() { return TNodeTwoTitle; }
cccb87ac-7d9d-449c-8f05-e357db947780
public void setTNodeTwo(String in) { TNodeTwoTitle = in; }
ac550a7d-6cd0-49fa-99f8-be8361eb2084
public String notMe(TNode in) { String ret = ""; if(TNodeOne.equals(in)) ret = TNodeTwoTitle; else ret = TNodeOne.getTitle(); return ret; }
c4f43ca5-15fa-41d1-8da9-303204d87100
public TNode isMe(TNode in) { return TNodeOne; }
2d54ad4c-25cf-4bd8-9078-b9f0e6e43b35
@Override public String toString() { return TNodeOne + "-"+this.weight +"-"+TNodeTwoTitle; }
6d444bba-ec2c-42f2-b6f5-d48a88d31108
@Override public boolean equals(Object o) { boolean areEqual = true; if(o.getClass().equals(this.getClass())) { TConnection c = (TConnection)o; areEqual = (c.getTNodeOne().equals(this.getTNodeOne()) && c.getTNodeTwo().equals(this.getTNodeTwo()) && c.getWeight() == this.weight); }else areEqual = false; return areEqual; }
22a77d54-f320-4f99-b1f5-72ac97483ffb
public static TConnection makeCopy(TConnection in) { TConnection toRet = new TConnection(); return toRet; }
c5c12811-0780-4d6a-8c2e-5ade9a27e48e
public static void main(String[] args) { TTree input = TreeInput.getTreeFromConsole(); // TTree input = TreeInput.getTreeFromFile(); System.out.println(input); TTree output = new PrimSolution(input).solveTree(); System.out.println("Got to end"); System.out.println(output); }
179b486c-510c-4f29-b4c0-c5facb80f91b
public PrimSolution(TTree inputTree) { this.tree = inputTree; }
4fd8c470-774a-44af-a8b8-bb1b739704d0
public TTree getTree() { return tree; }
1c907956-1821-435a-a4f5-dc8181d234ea
public void setTree(TTree tree) { this.tree = tree; }
901d80ae-16ff-44ae-a003-34b5caf5264f
public TTree solveTree() { // Take a vertex (randomly) TNode first = tree.getNodes().get(0); List<TNode> solution = new ArrayList<TNode>(); solution.add(first); while (solution.size() < tree.getNodes().size()) { TConnection lowest = new TConnection(null, null, Integer.MAX_VALUE); for (TNode n : solution) { for (TConnection c : n.getConnections()) { TNode temp = new TNode(c.getTNodeTwo()); boolean solHasNode = solution.contains(temp); boolean weightLower = c.getWeight() < lowest.getWeight(); if (!solHasNode && weightLower) { lowest = c; solution.add(TNode.copyNode(findNodeByName(lowest.getTNodeTwo()))); } } } } TTree solved = new TTree(); solved.addNodes(solution); return solved; }
f5b077df-2e4c-442f-a2b1-ecd3e87fff17
private TNode findNodeByName(String name) { TNode ret = null; for (TNode n : tree.getNodes()) { if (n.getTitle().equalsIgnoreCase(name)) { ret = n; break; } } return ret; }
c932eecb-020a-44fa-b265-4cf7fa8ba3cd
public static TTree getTreeFromConsole() { TTree tree = new TTree(); System.out .println("Please input connections in this tree in the following format:\n" + ": Title-NumericalWeight-Title2 or a-2-b\nDo not add any spaces or numbers (other than the weight)." + "\nType 'stop' when you are finished entering connections"); Scanner scan = new Scanner(System.in); String input = ""; while (!input.equalsIgnoreCase("stop")) { input = scan.nextLine(); List<TConnection> connect = ConnectionInput .getConnectionFromString(input); for (TConnection c : connect) { tree.addNode(c.getTNodeOne()); } } return tree; }
8c9c44c6-b078-4af9-a0be-09b2d6bc07d9
public static int getInt(String prompt) { int ret = 0; Scanner s = new Scanner(System.in); int input = -1; while (input == -1) { System.out.println(prompt); try { input = Integer.parseInt(s.nextLine()); } catch (IOError e) { System.out.println(e.getMessage()); } catch (NumberFormatException e) { System.out.println("Please input a valid positive integer"); } finally { s.close(); s = new Scanner(System.in); } } return ret; }
a4375874-735d-4389-b6a3-2d6b055c6cb8
public static String getString(String prompt) { String ret = null; Scanner s = new Scanner(System.in); while(ret == null) { System.out.println(prompt); ret = s.nextLine(); if(ret.isEmpty()) ret = null; } return ret; }
e3d1fc7a-444b-43c6-b085-dc3467d66440
public static List<TConnection> getConnectionFromString(String input) { List<TConnection> returnConnections = new ArrayList<TConnection>(); TConnection connectionOne = null; TConnection connectionTwo = null; if(input.matches(CONNECTION_PATTERN)) { String[] parts = input.split("-"); connectionOne = new TConnection(); connectionTwo = new TConnection(); if(parts != null && parts.length == 3) { TNode one = new TNode(parts[0]); connectionOne.setTNodeOne(one); try { int weight = Integer.parseInt(parts[1]); connectionOne.setWeight(weight); connectionTwo.setWeight(weight); }catch (NumberFormatException e) { System.out.println(e.getMessage()); connectionOne = null; connectionTwo = null; } TNode two = new TNode(parts[2]); connectionTwo.setTNodeOne(two); connectionTwo.setTNodeTwo(one.getTitle()); connectionOne.setTNodeTwo(two.getTitle()); one.addConnection(connectionOne); two.addConnection(connectionTwo); returnConnections.add(connectionOne); returnConnections.add(connectionTwo); } } return returnConnections; }
00deb46b-846e-4053-bcf8-f6921484871b
@Before public void init() { fibonacciCalc = new FibCalcImpl(); }
39391e55-1bee-40ae-b922-7bb5bf00a3ae
@Test public void testFib() { assertEquals(55, fibonacciCalc.fib(10)); assertEquals(6765, fibonacciCalc.fib(20)); }
273e4cc2-495c-451f-86ea-602d25a1be4c
public long fib(int n);
cb259d54-7c15-466a-aa3e-b6a7edef3637
public PerformanceTestResult(long totalTime, long minTime, long maxTime) { this.totalTime = totalTime; this.minTime = minTime; this.maxTime = maxTime; }
259e7ba0-a72b-4fa6-904a-bc932e9ee6f7
public long getMaxTime() { return maxTime; }
682ba7dd-3170-400a-83ec-235df3d2f178
public long getMinTime() { return minTime; }
011dfdc2-18b0-4d00-aa1f-81facdf5de4d
public long getTotalTime() { return totalTime; }
057bb831-70dc-467e-b25e-c11423e0dbf3
public PerformanceTestResult runPerformanceTest( Runnable task, int executionCount, int threadPoolSize) throws InterruptedException;
76a419c8-bf5c-46cc-894e-52c6688e1174
@Override public long fib(int n) { int[] fibArray = new int[n + 1]; fibArray[0] = 0; fibArray[1] = 1; for (int i = 2; i < fibArray.length; i++) { fibArray[i] = fibArray[i - 1] + fibArray[i - 2]; } return fibArray[fibArray.length - 1]; }
39d3496a-263d-469f-b627-974530a1e76a
public static void main(String[] args) throws InterruptedException { if (args.length != 3) throw new IllegalArgumentException("Please provide valid parameters: fibonacci number, task quantity, threads quantity"); try { Integer n = Integer.valueOf(args[0]); Integer numberOfTasks = Integer.valueOf(args[1]); Integer threadQuantity = Integer.valueOf(args[2]); if (n <= 0 || numberOfTasks <= 0 || threadQuantity <= 0) throw new IllegalArgumentException("All input parameters should be greater than 0"); startCalculation(n, numberOfTasks, threadQuantity); } catch (NumberFormatException ex) { throw new IllegalAccessError("You have to provide integer numbers as a parameters"); } }
2d452cc7-a96e-4c48-89fe-0730f5853964
public static void startCalculation(int n, int numberOfTasks, int threadsQuantity) throws InterruptedException { PerformanceTester performanceTester = new PerformanceTesterImpl(); PerformanceTestResult performanceTestResult = performanceTester.runPerformanceTest(createRunnableTask(n), numberOfTasks, threadsQuantity); System.out.println(String.format("Total execution time: %s", performanceTestResult.getTotalTime())); System.out.println(String.format("Minimum execution time: %s", performanceTestResult.getMinTime())); System.out.println(String.format("Maximum execution time: %s", performanceTestResult.getMaxTime())); }
f6d3da87-ce06-4fde-8c67-c0821afdca39
private static Runnable createRunnableTask(final int n) { return new Runnable() { @Override public void run() { FibCalc fibCalc = new FibCalcImpl(); fibCalc.fib(n); } }; }
ffb35e3c-d5ae-4690-988e-8481ea2b629f
@Override public void run() { FibCalc fibCalc = new FibCalcImpl(); fibCalc.fib(n); }
8669c0ac-2c48-4aeb-add5-391d06bb8b78
@Override public PerformanceTestResult runPerformanceTest(Runnable task, int executionCount, int threadPoolSize) throws InterruptedException { return reduce(map(task, executionCount, threadPoolSize)); }
84c1f982-7057-427d-8471-8b7ff17a6a43
private Long[] map(final Runnable task, final int executionCount, int threadPoolSize) throws InterruptedException { final Map<Thread, Long[]> calculationResults = new HashMap<>(); //Initialize working threads for (int i = 0; i < threadPoolSize; i++) { Thread thread = new Thread() { @Override public void run() { Long[] calcs = calculationResults.get(this); for (int j = 0; j < calcs.length; j++) { long startTime = System.currentTimeMillis(); task.run(); calcs[j] = System.currentTimeMillis() - startTime; } } }; calculationResults.put(thread, new Long[executionCount]); } //Start jobs for (Thread thread : calculationResults.keySet()) { thread.start(); } //Prepare results Long[] results = new Long[0]; for (Map.Entry<Thread, Long[]> entry : calculationResults.entrySet()) { entry.getKey().join(); results = ArrayUtils.addAll(results, entry.getValue()); } return results; }
f39550e3-088e-4af3-b0a2-7da7db1c84be
@Override public void run() { Long[] calcs = calculationResults.get(this); for (int j = 0; j < calcs.length; j++) { long startTime = System.currentTimeMillis(); task.run(); calcs[j] = System.currentTimeMillis() - startTime; } }
768e4486-efac-4e94-9dcb-bc9e51b5f73c
private PerformanceTestResult reduce(Long[] values) { long totalTime = 0; long minTime = 0; long maxTime = 0; for (Long result : values) { totalTime += result; minTime = minTime==0 ? result : Math.min(result, minTime); maxTime = Math.max(result, maxTime); } return new PerformanceTestResult(totalTime, minTime, maxTime); }
698c68cd-b3d0-477d-abb8-2f584309e841
public void setUp() throws Exception { Clock.setTime(1000); cache = new CacheMapImpl<>(); cache.setTimeToLive(TIME_TO_LIVE); }
41b6e520-57c6-43c1-8d57-4c8d42cfdb82
public void testExpiry() throws Exception { cache.put(1, "apple"); assertEquals("apple", cache.get(1)); assertFalse(cache.isEmpty()); Clock.setTime(3000); assertNull(cache.get(1)); assertTrue(cache.isEmpty()); }
523f1a59-6959-42f4-aca1-961f42d2d564
public void testSize() throws Exception { assertEquals(0, cache.size()); cache.put(1, "apple"); assertEquals(1, cache.size()); Clock.setTime(3000); assertEquals(0, cache.size()); }