BrightSide Workbench Full Report + Source Code
ContentContext.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 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.action.content;
20 
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.function.Supplier;
26 import org.amic.util.date.CheckDate;
27 
32 public class ContentContext {
33 
34  private static final Map<String, SingleContent> contexts = Collections.synchronizedMap(new HashMap<>());
35 
36  public static Object getContent(String context, Supplier onDefault) {
37  Object obj = getContent(context);
38  if(obj == null) {
39  obj = onDefault.get();
40  if(obj != null) {
41  putContent(context, obj);
42  }
43  }
44  return obj;
45  }
46 
47  public static Object getContent(String context) {
48  SingleContent sc = contexts.get(context);
49  return sc != null ? sc.getObject() : null;
50  }
51 
52  public static void putContent(String context, Object data) {
53  SingleContent sc = new SingleContent(new Date(), data);
54  contexts.put(context, sc);
55  }
56 
57  public static void removeContent(String context) {
58  contexts.remove(context);
59  purge();
60  }
61 
62  private static void purge() {
63  Date old = new CheckDate().addMonths(-1).getDate();
64  contexts.entrySet().stream().filter((entry) -> (entry.getValue().getDate().before(old))).forEachOrdered((entry) -> {
65  contexts.remove(entry.getKey());
66  });
67  }
68 
69 }
static void removeContent(String context)
static Object getContent(String context)
static Object getContent(String context, Supplier onDefault)
static void putContent(String context, Object data)