All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.uberfire.java.nio.base.AttrsStorageImpl Maven / Gradle / Ivy

There is a newer version: 7.74.1.Final
Show newest version
/*
 * Copyright 2015 Red Hat, Inc. and/or its affiliates.
 *
 * 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.uberfire.java.nio.base;

import java.util.HashMap;
import java.util.Map;

import org.uberfire.java.nio.file.attribute.AttributeView;
import org.uberfire.java.nio.file.attribute.BasicFileAttributeView;

import static java.util.Collections.unmodifiableMap;

public class AttrsStorageImpl implements AttrsStorage {

    final Properties content = new Properties();
    final Map viewsNameIndex = new HashMap();
    final Map, AttributeView> viewsTypeIndex = new HashMap, AttributeView>();

    @Override
    public AttrsStorage getAttrStorage() {
        return this;
    }

    @Override
    public  void addAttrView(final V view) {
        viewsNameIndex.put(view.name(),
                           view);
        if (view instanceof ExtendedAttributeView) {
            final ExtendedAttributeView extendedView = (ExtendedAttributeView) view;
            for (Class type : extendedView.viewTypes()) {
                viewsTypeIndex.put(type,
                                   view);
            }
        } else {
            viewsTypeIndex.put(view.getClass(),
                               view);
        }
    }

    @Override
    public  V getAttrView(final Class type) {
        return (V) viewsTypeIndex.get(type);
    }

    @Override
    public  V getAttrView(final String name) {
        return (V) viewsNameIndex.get(name);
    }

    @Override
    public void clear() {
        viewsNameIndex.clear();
        viewsTypeIndex.clear();
        content.clear();
    }

    @Override
    public Properties toProperties() {
        return buildProperties(false);
    }

    @Override
    public void loadContent(final Properties properties) {
        content.clear();
        for (final Map.Entry attr : properties.entrySet()) {
            content.put(attr.getKey(),
                        attr.getValue());
        }
    }

    @Override
    public Map getContent() {
        return unmodifiableMap(buildProperties(false));
    }

    @Override
    public Map getAllContent() {
        return unmodifiableMap(buildProperties(true));
    }

    private synchronized Properties buildProperties(boolean includesNonSerializable) {
        final Properties properties = new Properties(content);

        for (final Map.Entry view : viewsNameIndex.entrySet()) {
            if (includesNonSerializable ||
                    view.getValue() instanceof ExtendedAttributeView && ((ExtendedAttributeView) view.getValue()).isSerializable()) {
                final ExtendedAttributeView extendedView = (ExtendedAttributeView) view.getValue();
                for (final Map.Entry attr : extendedView.readAllAttributes().entrySet()) {
                    properties.put(attr.getKey(),
                                   attr.getValue());
                }
            }
        }

        return properties;
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy