BrightSide Workbench Full Report + Source Code
History.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.elephant.context;
20 
21 import java.util.Iterator;
22 import java.util.Objects;
23 import java.util.concurrent.BlockingDeque;
24 import java.util.concurrent.LinkedBlockingDeque;
25 
30 public class History {
31 
32  /* Utilities */
33 
34  public static void add(IConstructor constructor, String path) {
35  getInstance(constructor).add(path);
36  }
37 
38  public static String get(IConstructor constructor) {
39  return getInstance(constructor).get();
40  }
41 
42  public static String peek(IConstructor constructor) {
43  return getInstance(constructor).peek();
44  }
45 
46  public static String back(IConstructor constructor) {
47  History history = getInstance(constructor);
48  history.get(); // remove current
49  return history.get();
50  }
51 
52  public static String peekBack(IConstructor constructor) {
53  History history = getInstance(constructor);
54  return history.back();
55  }
56  /* History */
57 
58  public void add(String path) {
59  String last = HISTORY.peekLast();
60  if(!Objects.equals(last, path)) {
61  if(!HISTORY.offerLast(path)) {
62  HISTORY.pollFirst();
63  HISTORY.offerLast(path);
64  }
65  }
66  }
67 
68  public String get() {
69  return HISTORY.pollLast();
70  }
71 
72  public String peek() {
73  return HISTORY.peekLast();
74  }
75 
76  public String back() {
77  if(HISTORY.size() > 1) {
78  Iterator<String> it = HISTORY.descendingIterator();
79  it.next();
80  return it.next();
81  }
82  return HISTORY.peekLast();
83  }
84 
85  /* Stack pool */
86 
87  private final BlockingDeque<String> HISTORY = new LinkedBlockingDeque<>(50);
88 
89  /* Instance */
90 
91  public static History getInstance(IConstructor constructor) {
92  History history = (History) constructor.getSessionAttribute(HISTORY_CONTEXT);
93  if(history == null) {
94  history = new History();
95  constructor.setSessionAttribute(HISTORY_CONTEXT, history);
96  }
97  return history;
98  }
99 
100  private History() {}
101 
102  private static final String HISTORY_CONTEXT = "EL_HIS_CTX";
103 
104 }
static String get(IConstructor constructor)
Definition: History.java:38
static History getInstance(IConstructor constructor)
Definition: History.java:91
static String peek(IConstructor constructor)
Definition: History.java:42
static String back(IConstructor constructor)
Definition: History.java:46
static String peekBack(IConstructor constructor)
Definition: History.java:52
static void add(IConstructor constructor, String path)
Definition: History.java:34
void setSessionAttribute(String key, Object value)