1 /**
2 License:
3 	Boost Software License - Version 1.0 - August 17th, 2003
4 
5 	Permission is hereby granted, free of charge, to any person or organization
6 	obtaining a copy of the software and accompanying documentation covered by
7 	this license (the "Software") to use, reproduce, display, distribute,
8 	execute, and transmit the Software, and to prepare derivative works of the
9 	Software, and to permit third-parties to whom the Software is furnished to
10 	do so, all subject to the following:
11 	
12 	The copyright notices in the Software and this entire statement, including
13 	the above license grant, this restriction and the following disclaimer,
14 	must be included in all copies of the Software, in whole or in part, and
15 	all derivative works of the Software, unless such copies or derivative
16 	works are solely in the form of machine-executable object code generated by
17 	a source language processor.
18 	
19 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 	FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 	SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 	FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 	ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 	DEALINGS IN THE SOFTWARE.
26 
27 Authors:
28 	aermicioi
29 **/
30 module aermicioi.aedi.container.value_container;
31 
32 import aermicioi.aedi.container.container;
33 import aermicioi.aedi.storage.storage;
34 import aermicioi.aedi.storage.locator;
35 import aermicioi.aedi.storage.object_storage;
36 import aermicioi.aedi.exception.not_found_exception;
37 
38 /**
39 Value container for instantiated components.
40 
41 **/
42 class ValueContainer : Container, Storage!(Object, string) {
43     
44     private {
45         ObjectStorage!(Object, string) values;
46     }
47     
48     public {
49         
50 		/**
51 			Default constructor for ValueContainer
52 		**/
53         this() {
54             this.values = new ObjectStorage!(Object, string);
55         }
56         
57         /**
58 		Save an object in ValueContainer by identity.
59 		
60 		Params:
61 			identity = identity of element in Storage.
62 			object = element which is to be saved in Storage.
63 			
64 		Return:
65 			ValueContainer 
66 		**/
67         ValueContainer set(Object object, string identity) {
68         	this.values.set(object, identity);
69         
70         	return this;
71         }
72         
73         /**
74         Remove an object from ValueContainer with identity.
75         
76         Remove an object from ValueContainer with identity. If there is no element by provided identity, then no action is performed.
77         
78         Params:
79         	identity = the identity of element to be removed.
80         	
81     	Return:
82     		ValueContainer
83         **/
84         ValueContainer remove(string identity) {
85         	this.values.remove(identity);
86         
87         	return this;
88         }
89         
90         /**
91 		Get an Object that is associated with identity.
92 		
93 		Params:
94 			identity = the object id.
95 			
96 		Throws:
97 			NotFoundException in case if the element wasn't found.
98 		
99 		Returns:
100 			Object element if it is available.
101 		**/
102         Object get(string identity) {
103 
104             if (this.values.has(identity)) {
105                 return this.values.get(identity);
106             }
107 
108             throw new NotFoundException("Object by id " ~ identity ~ " not found.");
109         }
110         
111         /**
112         Check if an object is present in Locator by identity.
113         
114         Note:
115         	This check should be done for elements that locator actually contains, and
116         	not in chained locator.
117         Params:
118         	identity = identity of element.
119         	
120     	Returns:
121     		bool true if an element by key is present in Locator.
122         **/
123         bool has(in string identity) inout {
124             
125             return this.values.has(identity);
126         }
127         
128 		/**
129         Sets up the internal state of container.
130         
131         Sets up the internal state of container (Ex, for singleton container it will spawn all objects that locator contains).
132 		In case of value container it does nothing.
133         **/
134         ValueContainer instantiate() {
135             
136 //            We do nothing since all components is already instantiated.
137             return this;
138         }
139     }
140 }