|
@@ -1,119 +1,119 @@ |
1 |
|
- |
// This Source Code is in the Public Domain per: http://unlicense.org |
2 |
|
- |
package org.litesoft.aokeyhole.objects.support; |
3 |
|
- |
|
4 |
|
- |
import java.util.*; |
5 |
|
- |
|
6 |
|
- |
public class MetaDataOwnerManager<Owner extends MetaDataOwner, Owned extends AbstractNamedMetaData<Owner>> { |
7 |
|
- |
private Owner mOurOwner; |
8 |
|
- |
private Map<String, Owned> mOwned = new HashMap<String, Owned>(); |
9 |
|
- |
private Owned mUnNamed = null; |
10 |
|
- |
|
11 |
|
- |
public MetaDataOwnerManager( Owner pOurOwner ) { |
12 |
|
- |
mOurOwner = pOurOwner; |
13 |
|
- |
} |
14 |
|
- |
|
15 |
|
- |
public int getOwnedCount() { |
16 |
|
- |
return mOwned.size(); |
17 |
|
- |
} |
18 |
|
- |
|
19 |
|
- |
public void add( Owned pToBeOwned ) { |
20 |
|
- |
// System.out.println( "" + this + ".add: " + pToBeOwned ); |
21 |
|
- |
String name = pToBeOwned.getName(); |
22 |
|
- |
if ( name == null ) { |
23 |
|
- |
mUnNamed = pToBeOwned; |
24 |
|
- |
} else { |
25 |
|
- |
Object prev = mOwned.put( name, pToBeOwned ); |
26 |
|
- |
if ( prev != null ) { |
27 |
|
- |
throw new IllegalStateException( "add w/ dup name: " + name ); |
28 |
|
- |
} |
29 |
|
- |
} |
30 |
|
- |
pToBeOwned.setOwner( mOurOwner ); |
31 |
|
- |
} |
32 |
|
- |
|
33 |
|
- |
public boolean remove( Owned pCurrentlyOwned ) { |
34 |
|
- |
// System.out.println( "" + this + ".remove: " + pCurrentlyOwned ); |
35 |
|
- |
String name = pCurrentlyOwned.getName(); |
36 |
|
- |
if ( name == null ) { |
37 |
|
- |
throw new IllegalStateException( "remove w/ No name" ); |
38 |
|
- |
} |
39 |
|
- |
Object prev = mOwned.remove( name ); |
40 |
|
- |
if ( prev == null ) { |
41 |
|
- |
return false; |
42 |
|
- |
} |
43 |
|
- |
if ( prev != pCurrentlyOwned ) { |
44 |
|
- |
throw new IllegalStateException( "remove '" + pCurrentlyOwned + "', but removed dup: " + prev ); |
45 |
|
- |
} |
46 |
|
- |
pCurrentlyOwned.setOwner( null ); |
47 |
|
- |
mOurOwner.changed(); |
48 |
|
- |
return true; |
49 |
|
- |
} |
50 |
|
- |
|
51 |
|
- |
public boolean isUsedOwnedName( String pNewName ) { |
52 |
|
- |
return mOwned.containsKey( pNewName ); |
53 |
|
- |
} |
54 |
|
- |
|
55 |
|
- |
public void reKey( Owned pOwnedToBeReKeyed, String pNewName ) { |
56 |
|
- |
if ( mOurOwner != pOwnedToBeReKeyed.getOwner() ) { |
57 |
|
- |
throw new IllegalStateException( "reKey Failed not properly Owned" ); |
58 |
|
- |
} |
59 |
|
- |
String pOldName = pOwnedToBeReKeyed.getName(); |
60 |
|
- |
|
61 |
|
- |
if ( pOldName == null ) { |
62 |
|
- |
if ( pOwnedToBeReKeyed != mUnNamed ) { |
63 |
|
- |
throw new IllegalStateException( "reKey Failed from UnNamed" ); |
64 |
|
- |
} |
65 |
|
- |
} else { |
66 |
|
- |
Object curO = mOwned.remove( pOldName ); |
67 |
|
- |
if ( curO != pOwnedToBeReKeyed ) { |
68 |
|
- |
throw new IllegalStateException( "reKey Failed from: " + pOldName ); |
69 |
|
- |
} |
70 |
|
- |
} |
71 |
|
- |
Object newO = mOwned.put( pNewName, pOwnedToBeReKeyed ); |
72 |
|
- |
if ( newO != null ) { |
73 |
|
- |
throw new IllegalStateException( "reKey Failed to: " + pNewName ); |
74 |
|
- |
} |
75 |
|
- |
mOurOwner.changed(); |
76 |
|
- |
} |
77 |
|
- |
|
78 |
|
- |
public Collection<Owned> getEntries() { |
79 |
|
- |
return mOwned.values(); |
80 |
|
- |
} |
81 |
|
- |
|
82 |
|
- |
public Owned getEntry( String pName ) { |
83 |
|
- |
return (pName == null) ? mUnNamed : mOwned.get( pName ); |
84 |
|
- |
} |
85 |
|
- |
|
86 |
|
- |
public boolean available( String pName ) { |
87 |
|
- |
return !mOwned.containsKey( pName ); |
88 |
|
- |
} |
89 |
|
- |
|
90 |
|
- |
/** |
91 |
|
- |
* @return null or Error |
92 |
|
- |
*/ |
93 |
|
- |
public String validatePersistable() { |
94 |
|
- |
for ( Owned md : getEntries() ) { |
95 |
|
- |
String error = md.validatePersistable(); |
96 |
|
- |
if ( error != null ) { |
97 |
|
- |
return error; |
98 |
|
- |
} |
99 |
|
- |
} |
100 |
|
- |
return null; |
101 |
|
- |
} |
102 |
|
- |
|
103 |
|
- |
public void setRehydrated() { |
104 |
|
- |
for ( Owned md : getEntries() ) { |
105 |
|
- |
md.setRehydrated(); |
106 |
|
- |
} |
107 |
|
- |
} |
108 |
|
- |
|
109 |
|
- |
public void clearChangeIndicators() { |
110 |
|
- |
for ( Owned md : getEntries() ) { |
111 |
|
- |
md.clearChangeIndicators(); |
112 |
|
- |
} |
113 |
|
- |
} |
114 |
|
- |
|
115 |
|
- |
@Override |
116 |
|
- |
public String toString() { |
117 |
|
- |
return mOurOwner.toString(); |
118 |
|
- |
} |
119 |
|
- |
} |
|
1 |
+ |
// This Source Code is in the Public Domain per: http://unlicense.org |
|
2 |
+ |
package org.litesoft.aokeyhole.objects.support; |
|
3 |
+ |
|
|
4 |
+ |
import java.util.*; |
|
5 |
+ |
|
|
6 |
+ |
public class MetaDataOwnerManager<Owner extends MetaDataOwner, Owned extends AbstractNamedMetaData<Owner>> { |
|
7 |
+ |
private Owner mOurOwner; |
|
8 |
+ |
private Map<String, Owned> mOwned = new HashMap<String, Owned>(); |
|
9 |
+ |
private Owned mUnNamed = null; |
|
10 |
+ |
|
|
11 |
+ |
public MetaDataOwnerManager( Owner pOurOwner ) { |
|
12 |
+ |
mOurOwner = pOurOwner; |
|
13 |
+ |
} |
|
14 |
+ |
|
|
15 |
+ |
public int getOwnedCount() { |
|
16 |
+ |
return mOwned.size(); |
|
17 |
+ |
} |
|
18 |
+ |
|
|
19 |
+ |
public void add( Owned pToBeOwned ) { |
|
20 |
+ |
// System.out.println( "" + this + ".add: " + pToBeOwned ); |
|
21 |
+ |
String name = pToBeOwned.getName(); |
|
22 |
+ |
if ( name == null ) { |
|
23 |
+ |
mUnNamed = pToBeOwned; |
|
24 |
+ |
} else { |
|
25 |
+ |
Object prev = mOwned.put( name, pToBeOwned ); |
|
26 |
+ |
if ( prev != null ) { |
|
27 |
+ |
throw new IllegalStateException( "add w/ dup name: " + name ); |
|
28 |
+ |
} |
|
29 |
+ |
} |
|
30 |
+ |
pToBeOwned.setOwner( mOurOwner ); |
|
31 |
+ |
} |
|
32 |
+ |
|
|
33 |
+ |
public boolean remove( Owned pCurrentlyOwned ) { |
|
34 |
+ |
// System.out.println( "" + this + ".remove: " + pCurrentlyOwned ); |
|
35 |
+ |
String name = pCurrentlyOwned.getName(); |
|
36 |
+ |
if ( name == null ) { |
|
37 |
+ |
throw new IllegalStateException( "remove w/ No name" ); |
|
38 |
+ |
} |
|
39 |
+ |
Object prev = mOwned.remove( name ); |
|
40 |
+ |
if ( prev == null ) { |
|
41 |
+ |
return false; |
|
42 |
+ |
} |
|
43 |
+ |
if ( prev != pCurrentlyOwned ) { |
|
44 |
+ |
throw new IllegalStateException( "remove '" + pCurrentlyOwned + "', but removed dup: " + prev ); |
|
45 |
+ |
} |
|
46 |
+ |
pCurrentlyOwned.setOwner( null ); |
|
47 |
+ |
mOurOwner.changed(); |
|
48 |
+ |
return true; |
|
49 |
+ |
} |
|
50 |
+ |
|
|
51 |
+ |
public boolean isUsedOwnedName( String pNewName ) { |
|
52 |
+ |
return mOwned.containsKey( pNewName ); |
|
53 |
+ |
} |
|
54 |
+ |
|
|
55 |
+ |
public void reKey( Owned pOwnedToBeReKeyed, String pNewName ) { |
|
56 |
+ |
if ( mOurOwner != pOwnedToBeReKeyed.getOwner() ) { |
|
57 |
+ |
throw new IllegalStateException( "reKey Failed not properly Owned" ); |
|
58 |
+ |
} |
|
59 |
+ |
String pOldName = pOwnedToBeReKeyed.getName(); |
|
60 |
+ |
|
|
61 |
+ |
if ( pOldName == null ) { |
|
62 |
+ |
if ( pOwnedToBeReKeyed != mUnNamed ) { |
|
63 |
+ |
throw new IllegalStateException( "reKey Failed from UnNamed" ); |
|
64 |
+ |
} |
|
65 |
+ |
} else { |
|
66 |
+ |
Object curO = mOwned.remove( pOldName ); |
|
67 |
+ |
if ( curO != pOwnedToBeReKeyed ) { |
|
68 |
+ |
throw new IllegalStateException( "reKey Failed from: " + pOldName ); |
|
69 |
+ |
} |
|
70 |
+ |
} |
|
71 |
+ |
Object newO = mOwned.put( pNewName, pOwnedToBeReKeyed ); |
|
72 |
+ |
if ( newO != null ) { |
|
73 |
+ |
throw new IllegalStateException( "reKey Failed to: " + pNewName ); |
|
74 |
+ |
} |
|
75 |
+ |
mOurOwner.changed(); |
|
76 |
+ |
} |
|
77 |
+ |
|
|
78 |
+ |
public Collection<Owned> getEntries() { |
|
79 |
+ |
return mOwned.values(); |
|
80 |
+ |
} |
|
81 |
+ |
|
|
82 |
+ |
public Owned getEntry( String pName ) { |
|
83 |
+ |
return (pName == null) ? mUnNamed : mOwned.get( pName ); |
|
84 |
+ |
} |
|
85 |
+ |
|
|
86 |
+ |
public boolean available( String pName ) { |
|
87 |
+ |
return !mOwned.containsKey( pName ); |
|
88 |
+ |
} |
|
89 |
+ |
|
|
90 |
+ |
/** |
|
91 |
+ |
* @return null or Error |
|
92 |
+ |
*/ |
|
93 |
+ |
public String validatePersistable() { |
|
94 |
+ |
for ( Owned md : getEntries() ) { |
|
95 |
+ |
String error = md.validatePersistable(); |
|
96 |
+ |
if ( error != null ) { |
|
97 |
+ |
return error; |
|
98 |
+ |
} |
|
99 |
+ |
} |
|
100 |
+ |
return null; |
|
101 |
+ |
} |
|
102 |
+ |
|
|
103 |
+ |
public void setRehydrated() { |
|
104 |
+ |
for ( Owned md : getEntries() ) { |
|
105 |
+ |
md.setRehydrated(); |
|
106 |
+ |
} |
|
107 |
+ |
} |
|
108 |
+ |
|
|
109 |
+ |
public void clearChangeIndicators() { |
|
110 |
+ |
for ( Owned md : getEntries() ) { |
|
111 |
+ |
md.clearChangeIndicators(); |
|
112 |
+ |
} |
|
113 |
+ |
} |
|
114 |
+ |
|
|
115 |
+ |
@Override |
|
116 |
+ |
public String toString() { |
|
117 |
+ |
return mOurOwner.toString(); |
|
118 |
+ |
} |
|
119 |
+ |
} |