1 /* 2 * Copyright (c) 2008-2019 Emmanuel Dupuy. 3 * This project is distributed under the GPLv3 license. 4 * This is a Copyleft license that gives the user the right to use, 5 * copy and modify the code freely for non-commercial purposes. 6 */ 7 8 package org.jd.gui.model.history; 9 10 import java.net.URI; 11 import java.util.ArrayList; 12 13 public class History { 14 protected URI current = null; 15 protected ArrayList<URI> backward = new ArrayList<>(); 16 protected ArrayList<URI> forward = new ArrayList<>(); 17 add(URI uri)18 public void add(URI uri) { 19 if (current == null) { 20 // Init history 21 forward.clear(); 22 current = uri; 23 return; 24 } 25 26 if (current.equals(uri)) { 27 // Already stored -> Nothing to do 28 return; 29 } 30 31 if (uri.getPath().toString().equals(current.getPath().toString())) { 32 if ((uri.getFragment() == null) && (uri.getQuery() == null)) { 33 // Ignore 34 } else if ((current.getFragment() == null) && (current.getQuery() == null)) { 35 // Replace current URI 36 current = uri; 37 } else { 38 // Store URI 39 forward.clear(); 40 backward.add(current); 41 current = uri; 42 } 43 return; 44 } 45 46 if (uri.toString().startsWith(current.toString())) { 47 // Replace current URI 48 current = uri; 49 return; 50 } 51 52 if (current.toString().startsWith(uri.toString())) { 53 // Parent URI -> Nothing to do 54 return; 55 } 56 57 // Store URI 58 forward.clear(); 59 backward.add(current); 60 current = uri; 61 } 62 backward()63 public URI backward() { 64 if (! backward.isEmpty()) { 65 forward.add(current); 66 int size = backward.size(); 67 current = backward.remove(size-1); 68 } 69 return current; 70 } 71 forward()72 public URI forward() { 73 if (! forward.isEmpty()) { 74 backward.add(current); 75 int size = forward.size(); 76 current = forward.remove(size-1); 77 } 78 return current; 79 } 80 canBackward()81 public boolean canBackward() { return !backward.isEmpty(); } canForward()82 public boolean canForward() { return !forward.isEmpty(); } 83 } 84