LightJason - AgentSpeak(L++)
CRelocateVariable.java
Go to the documentation of this file.
1 /*
2  * @cond LICENSE
3  * ######################################################################################
4  * # LGPL License #
5  * # #
6  * # This file is part of the LightJason AgentSpeak(L++) #
7  * # Copyright (c) 2015-19, LightJason (info@lightjason.org) #
8  * # This program is free software: you can redistribute it and/or modify #
9  * # it under the terms of the GNU Lesser General Public License as #
10  * # published by the Free Software Foundation, either version 3 of the #
11  * # License, or (at your option) any later version. #
12  * # #
13  * # This program is distributed in the hope that it will be useful, #
14  * # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15  * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16  * # GNU Lesser General Public License for more details. #
17  * # #
18  * # You should have received a copy of the GNU Lesser General Public License #
19  * # along with this program. If not, see http://www.gnu.org/licenses/ #
20  * ######################################################################################
21  * @endcond
22  */
23 
24 package org.lightjason.agentspeak.language.variable;
25 
30 
31 import javax.annotation.Nonnull;
32 import javax.annotation.Nullable;
33 import java.text.MessageFormat;
34 import java.util.Objects;
35 
36 
42 public final class CRelocateVariable<T> extends CVariable<T> implements IRelocateVariable
43 {
47  private static final long serialVersionUID = 2204692497385064257L;
51  private final IVariable<?> m_relocate;
52 
53 
59  public CRelocateVariable( final IVariable<?> p_variable )
60  {
61  super( p_variable.functor(), p_variable.raw() );
62  m_relocate = p_variable;
63  }
64 
70  public CRelocateVariable( @Nonnull final IPath p_functor, @Nonnull final IVariable<?> p_relocate )
71  {
72  super( p_functor, p_relocate.raw() );
73  m_relocate = p_relocate;
74  }
75 
82  private CRelocateVariable( @Nonnull final IPath p_functor, @Nonnull final IVariable<?> p_variable, @Nullable final T p_value )
83  {
84  super( p_functor, p_value );
85  m_relocate = p_variable;
86  }
87 
88  @Nonnull
89  @Override
90  public final IVariable<?> relocate()
91  {
92  return
93  m_relocate instanceof CConstant<?>
94  ? m_relocate
95  : m_relocate.set( this.raw() );
96  }
97 
98  @Nonnull
99  @Override
100  public final IVariable<T> shallowcopy( final IPath... p_prefix )
101  {
102  return ( Objects.isNull( p_prefix ) ) || ( p_prefix.length == 0 )
104  : new CRelocateVariable<>( p_prefix[0].append( m_functor ), m_relocate, m_value );
105  }
106 
107  @Nonnull
108  @Override
110  {
112  }
113 
114  @Nonnull
115  @Override
116  public final ITerm deepcopy( final IPath... p_prefix )
117  {
118  return new CRelocateVariable<>(
119  ( Objects.isNull( p_prefix ) ) || ( p_prefix.length == 0 )
120  ? m_functor
121  : m_functor.append( p_prefix[0] ),
122  m_relocate, CCommon.deepclone( m_value )
123  );
124  }
125 
126  @Nonnull
127  @Override
128  public final ITerm deepcopysuffix()
129  {
130  return new CRelocateVariable<>( CPath.from( m_functor.suffix() ), m_relocate, CCommon.deepclone( m_value ) );
131  }
132 
133  @Override
134  public String toString()
135  {
136  return MessageFormat.format( "{0}({1})>{2}", m_functor, Objects.isNull( m_value ) ? "" : m_value, m_relocate );
137  }
138 }
final IVariable< T > shallowcopysuffix()
clones the object (shallow-copy) without full-qualified path, only suffix is used ...
CRelocateVariable( @Nonnull final IPath p_functor, @Nonnull final IVariable<?> p_variable, @Nullable final T p_value)
private ctor for creating object-copy
final IVariable< T > shallowcopy(final IPath... p_prefix)
IVariable< T > set( @Nullable final T p_value)
sets the value
common structure for execution definition
interface for relocated variables (linkage between two variables for transfering the value) ...
static IPath from( @Nonnull final String p_string)
factor method to build path
Definition: CPath.java:166
class to create a path structure
Definition: CPath.java:53
final IPath m_functor
variable / functor name
Definition: CVariable.java:55
final IVariable<?> m_relocate
reference to relocated variable
final IVariable<?> relocate()
sets the value into the relocated variable and returns the modifed variable
IPath append( @Nonnull final IPath p_path)
appends a path at the current and returns a new object
String suffix()
returns the last part of the path
CRelocateVariable( @Nonnull final IPath p_functor, @Nonnull final IVariable<?> p_relocate)
ctor
public< N > N raw()
cast to any raw value type
Definition: CVariable.java:208