BrightSide Workbench Full Report + Source Code
SessionTags.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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.web.tags;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.stream.Collectors;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26 import org.turro.elephant.context.AbstractSessionObject;
27 import org.turro.elephant.context.IConstructor;
28 import org.turro.elephant.web.context.WebContext;
29 import org.turro.string.Strings;
30 
35 public class SessionTags extends AbstractSessionObject<SessionTags> {
36 
37  public List<String> tags() {
38  return tags;
39  }
40 
41  public String path() {
42  return tags.stream().collect(Collectors.joining("/"));
43  }
44 
45  public String first() {
46  return !tags.isEmpty() ? tags.get(0) : null;
47  }
48 
49  public String current() {
50  return !tags.isEmpty() ? tags.get(tags.size() - 1) : null;
51  }
52 
53  public String previous() {
54  return tags.size() > 1 ? tags.get(tags.size() - 2) : null;
55  }
56 
57  public void removeLast() {
58  if(!tags.isEmpty()) {
59  tags.remove(tags.size() -1);
60  }
61  }
62 
63  public void add(WebContext context) {
64  if(context != null) {
65  add(context.getWebTag());
66  }
67  }
68 
69  public void add(String tag) {
70  if(!Strings.isBlank(tag)) {
71  tags.add(tag);
72  }
73  }
74 
75  /* Factory */
76 
77  public static String key() {
78  return "web-tags";
79  }
80 
81  public static SessionTags get() {
82  return AbstractSessionObject.get(key(), () -> new SessionTags());
83  }
84 
85  public static SessionTags get(HttpServletRequest request, HttpServletResponse response) {
86  return AbstractSessionObject.get(request, response, key(), () -> new SessionTags());
87  }
88 
89  public static SessionTags get(IConstructor constructor) {
90  return AbstractSessionObject.get(constructor, key(), () -> new SessionTags());
91  }
92 
93  public static void remove() {
95  }
96 
97  public static void remove(HttpServletRequest request, HttpServletResponse response) {
98  AbstractSessionObject.remove(request, response, key());
99  }
100 
101  public static void remove(IConstructor constructor) {
102  AbstractSessionObject.remove(constructor, key());
103  }
104 
105  private final List<String> tags;
106 
107  private SessionTags() {
108  this.tags = new ArrayList<>();
109  }
110 
111 }
static< E > E get(String key, Supplier< E > supplier)