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

org.drools.core.metadata.NewInstanceLiteral Maven / Gradle / Ivy

There is a newer version: 9.44.0.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.
 * 
 *      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.drools.core.metadata;

import org.drools.core.factmodel.traits.InstantiatorFactory;

import java.net.URI;
import java.util.UUID;

public abstract class NewInstanceLiteral extends AbstractWMTask implements NewInstance {
    protected URI uri;
    protected T result;

    protected ModifyLiteral setter;
    protected InstantiatorFactory instantiatorFactory;

    protected With[] with;

    public NewInstanceLiteral( With... args ) {
        this.with = args;
    }

    public NewInstanceLiteral( Object identifier, With... args ) {
        this.uri = URI.create( identifier.toString() );
        this.with = args;
    }

    public boolean isInterface() {
        return false;
    }

    @Override
    public KIND kind() {
        return KIND.ASSERT;
    }

    @Override
    public Object getTargetId() {
        return uri;
    }

    @Override
    public Object callUntyped() {
        return construct();
    }

    @Override
    public Modify getInitArgs() {
        return setter;
    }

    @Override
    public T call() {
        result = (T) construct();
        if ( setter != null ) {
            setter.setTarget( result );
            setter.call();
        }
        return result;
    }

    protected abstract Object construct();

    protected void constructId( Class klass ) {
        if ( uri == null ) {
            uri = URI.create( getInstantiatorFactory() != null ?
                              getInstantiatorFactory().createId( klass ).toString() :
                              UUID.randomUUID().toString()
            );
        }

    }

    public InstantiatorFactory getInstantiatorFactory() {
        return instantiatorFactory;
    }

    public NewInstance setInstantiatorFactory( InstantiatorFactory instantiatorFactory ) {
        this.instantiatorFactory = instantiatorFactory;
        return this;
    }

    @Override
    public URI getUri() {
        return URI.create( uri.toString() + "?create" );
    }

    @Override
    public Object getId() {
        return uri;
    }

    @Override
    public ModifyLiteral getSetters() {
        return setter;
    }

    @Override
    public T getTarget() {
        return result;
    }


    @Override
    public int hashCode() {
        return System.identityHashCode( this );
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy