/* ***************************************************************************
 * Copyright (c) 2008 Brabenetz Harald, Austria.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 *****************************************************************************/
package org.settings4j.contentresolver;

import org.settings4j.ContentResolver;

/**
 * The UnionContentResolver can be an container for many other {@link ContentResolver}
 * which will be processed in sequence.
 * <p>
 * the first result of a ContentResolver which are a not-null value will be used as result.
 * 
 * @author Harald.Brabenetz
 */
public class UnionContentResolver implements ContentResolver {

    private ContentResolver[] contentResolvers = new ContentResolver[0];

    /**
     * Default Constructor.
     */
    public UnionContentResolver() {
        super();
    }

    /**
     * Constructor with the first {@link ContentResolver}.
     * 
     * @param contentResolver with the first ContentResolver.
     */
    public UnionContentResolver(final ContentResolver contentResolver) {
        super();
        addContentResolver(contentResolver);
    }

    /** {@inheritDoc} */
    public synchronized void addContentResolver(final ContentResolver contentResolver) {

        final ContentResolver[] contentResolversNew = new ContentResolver[this.contentResolvers.length + 1];
        for (int i = 0; i < this.contentResolvers.length; i++) {
            contentResolversNew[i] = this.contentResolvers[i];
        }
        contentResolversNew[this.contentResolvers.length] = contentResolver;

        this.contentResolvers = contentResolversNew;
    }

    /** {@inheritDoc} */
    public byte[] getContent(final String key) {
        byte[] result = null;
        for (int i = 0; i < this.contentResolvers.length; i++) {
            result = this.contentResolvers[i].getContent(key);
            if (result != null) {
                return result;
            }
        }
        return result;
    }
}

@@ -1731,21 +1731,8 @@
 lic 
-synchronized 
 void
@@ -1789,26 +1789,59 @@
 esolver) %7B%0D%0A
-%0D%0A
+        synchronized (this) %7B%0D%0A    
         fina
@@ -1938,32 +1938,36 @@
  + 1%5D;%0D%0A        
+   
+ 
 for (int i = 0; 
@@ -2012,32 +2012,36 @@
  %7B%0D%0A            
+    
 contentResolvers
@@ -2072,24 +2072,28 @@
 olvers%5Bi%5D;%0D%0A
+    
         %7D%0D%0A 
@@ -2091,32 +2091,36 @@
      %7D%0D%0A        
+    
 contentResolvers
@@ -2183,16 +2183,20 @@
         
+    
 this.con
@@ -2233,16 +2233,27 @@
 rsNew;%0D%0A
+        %7D%0D%0A
     %7D%0D%0A%0D

improve synchronized