001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.schematic; 017 018import java.util.Collection; 019import org.modeshape.schematic.document.Document; 020import org.modeshape.schematic.document.EditableArray; 021import org.modeshape.schematic.document.EditableDocument; 022import org.modeshape.schematic.internal.document.ArrayEditor; 023import org.modeshape.schematic.internal.document.BasicArray; 024import org.modeshape.schematic.internal.document.BasicDocument; 025import org.modeshape.schematic.internal.document.DefaultDocumentValueFactory; 026import org.modeshape.schematic.internal.document.DocumentEditor; 027import org.modeshape.schematic.internal.document.DocumentValueFactory; 028 029/** 030 * Factory class that creates {@link EditableDocument} instances 031 * 032 * @author Horia Chiorean (hchiorea@redhat.com) 033 */ 034public class DocumentFactory { 035 036 protected static DocumentValueFactory DEFAULT_FACTORY = DefaultDocumentValueFactory.INSTANCE; 037 038 /** 039 * Create a new editable document that is a copy of the supplied document. 040 * 041 * @param original the original document 042 * @return the editable document; never null 043 */ 044 public static EditableDocument newDocument( Document original ) { 045 BasicDocument newDoc = new BasicDocument(); 046 newDoc.putAll(original); 047 return new DocumentEditor(newDoc, DEFAULT_FACTORY); 048 } 049 050 /** 051 * Create a new editable document that can be used as a new document entry in a SchematicDb or as nested documents for other 052 * documents. 053 * 054 * @return the editable document; never null 055 */ 056 public static EditableDocument newDocument() { 057 return new DocumentEditor(new BasicDocument(), DEFAULT_FACTORY); 058 } 059 060 /** 061 * Create a new editable document, initialized with a single field, that can be used as a new document entry in a SchematicDb 062 * or as nested documents for other documents. 063 * 064 * @param name the name of the initial field in the resulting document; if null, the field will not be added to the returned 065 * document 066 * @param value the value of the initial field in the resulting document 067 * @return the editable document; never null 068 */ 069 public static EditableDocument newDocument( String name, 070 Object value ) { 071 return new DocumentEditor(new BasicDocument(name, value), DEFAULT_FACTORY); 072 } 073 074 /** 075 * Create a new editable document, initialized with two fields, that can be used as a new document entry in a SchematicDb or 076 * as nested documents for other documents. 077 * 078 * @param name1 the name of the first field in the resulting document; if null, the field will not be added to the returned 079 * document 080 * @param value1 the value of the first field in the resulting document 081 * @param name2 the name of the second field in the resulting document; if null, the field will not be added to the returned 082 * document 083 * @param value2 the value of the second field in the resulting document 084 * @return the editable document; never null 085 */ 086 public static EditableDocument newDocument( String name1, 087 Object value1, 088 String name2, 089 Object value2 ) { 090 return new DocumentEditor(new BasicDocument(name1, value1, name2, value2), DEFAULT_FACTORY); 091 } 092 093 /** 094 * Create a new editable document, initialized with three fields, that can be used as a new document entry in a SchematicDb or 095 * as nested documents for other documents. 096 * 097 * @param name1 the name of the first field in the resulting document; if null, the field will not be added to the returned 098 * document 099 * @param value1 the value of the first field in the resulting document 100 * @param name2 the name of the second field in the resulting document; if null, the field will not be added to the returned 101 * document 102 * @param value2 the value of the second field in the resulting document 103 * @param name3 the name of the third field in the resulting document; if null, the field will not be added to the returned 104 * document 105 * @param value3 the value of the third field in the resulting document 106 * @return the editable document; never null 107 */ 108 public static EditableDocument newDocument( String name1, 109 Object value1, 110 String name2, 111 Object value2, 112 String name3, 113 Object value3 ) { 114 return new DocumentEditor(new BasicDocument(name1, value1, name2, value2, name3, value3), DEFAULT_FACTORY); 115 } 116 117 /** 118 * Create a new editable document, initialized with four fields, that can be used as a new document entry in a SchematicDb or 119 * as nested documents for other documents. 120 * 121 * @param name1 the name of the first field in the resulting document; if null, the field will not be added to the returned 122 * document 123 * @param value1 the value of the first field in the resulting document 124 * @param name2 the name of the second field in the resulting document; if null, the field will not be added to the returned 125 * document 126 * @param value2 the value of the second field in the resulting document 127 * @param name3 the name of the third field in the resulting document; if null, the field will not be added to the returned 128 * document 129 * @param value3 the value of the third field in the resulting document 130 * @param name4 the name of the fourth field in the resulting document; if null, the field will not be added to the returned 131 * document 132 * @param value4 the value of the fourth field in the resulting document 133 * @return the editable document; never null 134 */ 135 public static EditableDocument newDocument( String name1, 136 Object value1, 137 String name2, 138 Object value2, 139 String name3, 140 Object value3, 141 String name4, 142 Object value4 ) { 143 return new DocumentEditor(new BasicDocument(name1, value1, name2, value2, name3, value3, name4, value4), DEFAULT_FACTORY); 144 } 145 146 /** 147 * Create a new, empty editable array that can be used as a new array value in other documents. 148 * 149 * @return the editable array; never null 150 */ 151 public static EditableArray newArray() { 152 return new ArrayEditor(new BasicArray(), DEFAULT_FACTORY); 153 } 154 155 /** 156 * Create a new, empty editable array that can be used as a new array value in other documents. 157 * 158 * @param initialCapacity the initial allocated capacity for the array 159 * @return the editable array; never null 160 */ 161 public static EditableArray newArray( int initialCapacity ) { 162 return new ArrayEditor(new BasicArray(initialCapacity), DEFAULT_FACTORY); 163 } 164 165 /** 166 * Create a new editable array that can be used as a new array value in other documents. 167 * 168 * @param values the initial values for the array 169 * @return the editable array; never null 170 */ 171 public static EditableArray newArray( Collection<?> values ) { 172 BasicArray array = new BasicArray(values.size()); 173 array.addAllValues(values); 174 return new ArrayEditor(array, DEFAULT_FACTORY); 175 } 176 177 /** 178 * Create a new editable array that can be used as a new array value in other documents. 179 * 180 * @param values the initial values for the array 181 * @return the editable array; never null 182 */ 183 public static EditableArray newArray( Object... values ) { 184 BasicArray array = new BasicArray(); 185 for (Object value : values) { 186 array.addValue(value); 187 } 188 return new ArrayEditor(array, DEFAULT_FACTORY); 189 } 190}