blob: a0aaeec6aafce4a978656b7a429b41ddae2efb13 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
/*
* Copyright (c) 2022 Eike Stepper (Loehne, Germany) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eike Stepper - initial API and implementation
*/
package org.eclipse.net4j.util.container;
import org.eclipse.net4j.util.io.IORuntimeException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/**
* An abstract implementation of a {@link Container container}.
*
* @since 3.18
* @author Eike Stepper
*/
public abstract class PersistableContainer<E> extends ModifiableContainer<E> implements IContainer.Persistable<E>
{
private Persistence<E> persistence;
public PersistableContainer(Class<E> componentType)
{
super(componentType);
}
@Override
public final Persistence<E> getPersistence()
{
return persistence;
}
/**
* @since 3.5
*/
@Override
public final void setPersistence(Persistence<E> persistence)
{
this.persistence = persistence;
}
public boolean isSavedWhenModified()
{
return true;
}
@Override
public synchronized void load() throws IORuntimeException
{
if (persistence != null)
{
Collection<E> elements = persistence.loadElements();
clear();
addAllElements(elements);
}
}
/**
* @since 3.5
*/
@Override
public synchronized void save() throws IORuntimeException
{
if (persistence != null)
{
List<E> elements = Arrays.asList(getElements());
persistence.saveElements(elements);
}
}
@Override
protected void doActivate() throws Exception
{
super.doActivate();
load();
}
@Override
protected void doDeactivate() throws Exception
{
if (!isSavedWhenModified())
{
save();
}
super.doDeactivate();
}
@Override
protected void containerModified()
{
if (isSavedWhenModified())
{
save();
}
}
}
|