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

com.speedment.tool.propertyeditor.internal.component.PropertyEditorComponentImpl Maven / Gradle / Ivy

Go to download

A Speedment bundle that shades all dependencies into one jar. This is useful when deploying an application on a server.

The newest version!
/*
 *
 * Copyright (c) 2006-2019, Speedment, Inc. All Rights Reserved.
 *
 * 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 com.speedment.tool.propertyeditor.internal.component;

import com.speedment.common.injector.Injector;
import com.speedment.common.injector.annotation.ExecuteBefore;
import com.speedment.common.injector.annotation.Inject;
import com.speedment.common.mapstream.MapStream;
import com.speedment.runtime.config.*;
import com.speedment.runtime.config.trait.*;
import com.speedment.tool.config.*;
import com.speedment.tool.config.trait.*;
import com.speedment.tool.propertyeditor.PropertyEditor;
import com.speedment.tool.propertyeditor.component.PropertyEditorComponent;
import com.speedment.tool.propertyeditor.editor.*;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Stream;

import static com.speedment.common.injector.State.INITIALIZED;

/**
 *
 * @author Simon Jonasson
 * @since  3.0.0
 */
public final class PropertyEditorComponentImpl implements PropertyEditorComponent {
    
    private final Map, Map>>> factoryMap; // Ordered based on insert
    private @Inject Injector injector;
    
    public PropertyEditorComponentImpl (){
        this.factoryMap = Collections.synchronizedMap(new LinkedHashMap<>());
    }
    
    @ExecuteBefore(INITIALIZED)
    void installEditors() {
        install(HasEnabledProperty.class,    HasEnableUtil.ENABLED,    EnabledPropertyEditor::new);
        install(HasNameProperty.class,       HasNameUtil.NAME,          NamePropertyEditor::new);
        install(HasAliasProperty.class,      HasAliasUtil.ALIAS,        AliasPropertyEditor::new);
        install(HasNullableProperty.class,   HasNullableUtil.NULLABLE,  NullablePropertyEditor::new);
        install(HasOrderTypeProperty.class,  HasOrderTypeUtil.ORDER_TYPE, OrderTypePropertyEditor::new);
        install(ColumnProperty.class,        ColumnUtil.AUTO_INCREMENT, AutoIncrementPropertyEditor::new);
        install(HasTypeMapperProperty.class, HasTypeMapperUtil.TYPE_MAPPER, TypeMapperPropertyEditor::new);
        install(DbmsProperty.class,          DbmsUtil.TYPE_NAME,        DbmsTypePropertyEditor::new);
        install(DbmsProperty.class,          DbmsUtil.IP_ADDRESS,       IpAdressPropertyEditor::new);
        install(DbmsProperty.class,          DbmsUtil.PORT,             PortNumberEditor::new);
        install(DbmsProperty.class,          DbmsUtil.USERNAME,         UsernamePropertyEditor::new);
        install(DbmsProperty.class,          DbmsUtil.CONNECTION_URL,   ConnectionUrlPropertyEditor::new);
        install(IndexProperty.class,         IndexUtil.UNIQUE,          UniquePropertyEditor::new);
        install(ProjectProperty.class,       ProjectUtil.COMPANY_NAME,           CompanyNamePropertyEditor::new);
        install(ProjectProperty.class,       ProjectUtil.PACKAGE_LOCATION,       PackageLocationPropertyEditor::new);
        install(HasPackageNameProperty.class,   HasPackageNameUtil.PACKAGE_NAME,          PackageNameEditor::new);
        install(ForeignKeyColumnProperty.class, ForeignKeyColumnUtil.FOREIGN_COLUMN_NAME, ForeignKeyColumnEditor::new);
    }
    
    @Override
    public  Stream getUiVisibleProperties(DOC document) {
        return MapStream.of(factoryMap)                                         // MapStream>>
            .filterKey(clazz -> clazz.isAssignableFrom(document.getClass()))    // MapStream>>
            .flatMapValue(m -> MapStream.of(m))                                 // MapStream>>
            .mapKey((clazz, entry) -> entry.getKey())                           // MapStream>>
            .mapValue(Map.Entry::getValue)                                      // MapStream>
            .distinctKeys((a, b) -> b)                                          // If keys collide, keep newest
            .values()                                                           // Stream>
            .map(Supplier::get)                                                 // Stream  (pre inject)
            .map(injector::inject)                                              // Stream  (post inject)
            .map(factory -> {                                                   // Stream>
                @SuppressWarnings("unchecked")
                final PropertyEditor casted =
                    (PropertyEditor) factory;
                return casted;
            })
            .flatMap(factory -> factory.fieldsFor(document));                   // Stream
    }

    @Override
    public  void install(Class documentType, String propertyKey, Supplier> factory) {
        aquire(documentType).put(propertyKey, factory);
    }
    
    private  Map>> aquire(Class documentType){
        @SuppressWarnings("unchecked")
        final Map>> result = (Map>>) (Object)
            factoryMap.computeIfAbsent(
                documentType, 
                d -> Collections.synchronizedMap( new LinkedHashMap<>() )
            );
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy