LightJason - AgentSpeak(L++)
CVariable.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 
26 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
33 
34 import javax.annotation.Nonnull;
35 import javax.annotation.Nullable;
36 import java.text.MessageFormat;
37 import java.util.Arrays;
38 import java.util.Objects;
39 
40 
46 public class CVariable<T> implements IVariable<T>
47 {
51  private static final long serialVersionUID = -5542578381343603600L;
55  protected final IPath m_functor;
59  protected final boolean m_any;
63  protected T m_value;
64 
70  public CVariable( @Nonnull final String p_functor )
71  {
72  this( CPath.from( p_functor ), null );
73  }
74 
81  public CVariable( @Nonnull final String p_functor, @Nullable final T p_value )
82  {
83  this( CPath.from( p_functor ), p_value );
84  }
85 
91  public CVariable( @Nonnull final IPath p_functor )
92  {
93  this( p_functor, null );
94  }
95 
102  @SuppressFBWarnings( "EC_UNRELATED_CLASS_AND_INTERFACE" )
103  public CVariable( @Nonnull final IPath p_functor, @Nullable final T p_value )
104  {
105  m_any = p_functor.empty() || p_functor.equals( "_" );
106  m_functor = p_functor;
107  this.internalset( p_value );
108  }
109 
110  @Nonnull
111  @Override
112  public IVariable<T> set( @Nullable final T p_value )
113  {
114  return this.internalset( p_value );
115  }
116 
117  @Override
118  public boolean allocated()
119  {
120  return m_value != null;
121  }
122 
123  @Override
124  public final boolean any()
125  {
126  return m_any;
127  }
128 
129  @Override
130  public boolean mutex()
131  {
132  return false;
133  }
134 
135  @Nonnull
136  @Override
137  public IVariable<T> thrownotallocated() throws IllegalStateException
138  {
139  if ( !this.allocated() )
141 
142  return this;
143  }
144 
145  @Override
146  public boolean valueassignableto( @Nonnull final Class<?>... p_class )
147  {
148  return Objects.isNull( m_value ) || Arrays.stream( p_class ).anyMatch( i -> i.isAssignableFrom( m_value.getClass() ) );
149  }
150 
151  @Nonnull
152  @Override
153  public IVariable<T> throwvaluenotassignableto( @Nonnull final Class<?>... p_class ) throws IllegalArgumentException
154  {
155  if ( !this.valueassignableto( p_class ) )
156  throw new CIllegalArgumentException( CCommon.languagestring( this, "notassignable", m_functor, Arrays.asList( p_class ) ) );
157 
158  return this;
159  }
160 
161  @Override
162  public final int hashCode()
163  {
164  return m_functor.hashCode();
165  }
166 
167  @Override
168  public final boolean equals( final Object p_object )
169  {
170  return ( p_object instanceof IVariable<?> ) && ( this.hashCode() == p_object.hashCode() );
171  }
172 
173  @Override
174  public String toString()
175  {
176  return MessageFormat.format( "{0}({1})", m_functor, Objects.isNull( m_value ) ? "" : m_value );
177  }
178 
179  @Nonnull
180  @Override
181  public final String functor()
182  {
183  return m_functor.suffix();
184  }
185 
186  @Nonnull
187  @Override
188  public final IPath functorpath()
189  {
190  return m_functor.subpath( 0, m_functor.size() - 1 );
191  }
192 
193  @Nonnull
194  @Override
195  public final IPath fqnfunctor()
196  {
197  return m_functor;
198  }
199 
200  @Override
201  public final boolean hasVariable()
202  {
203  return true;
204  }
205 
206  @Override
207  @SuppressWarnings( "unchecked" )
208  public <N> N raw()
209  {
210  return (N) m_value;
211  }
212 
213  @Nonnull
214  @Override
215  public IVariable<T> shallowcopy( final IPath... p_prefix )
216  {
217  return ( Objects.isNull( p_prefix ) ) || ( p_prefix.length == 0 )
218  ? new CVariable<>( m_functor, m_value )
219  : new CVariable<>( p_prefix[0].append( m_functor ), m_value );
220  }
221 
222  @Nonnull
223  @Override
225  {
226  return new CVariable<>( m_functor.suffix(), m_value );
227  }
228 
229  @Nonnull
230  @Override
231  public ITerm deepcopy( final IPath... p_prefix )
232  {
233  return new CVariable<>(
234  ( Objects.isNull( p_prefix ) ) || ( p_prefix.length == 0 )
235  ? m_functor
236  : m_functor.append( p_prefix[0] ),
238  );
239  }
240 
241  @Nonnull
242  @Override
244  {
246  }
247 
248  @Override
249  public final int structurehash()
250  {
251  return 0;
252  }
253 
260  @SuppressWarnings( "unchecked" )
261  private IVariable<T> internalset( final T p_value )
262  {
263  // value must be set manually to avoid exception throwing (see CVariable.set)
264  if ( !m_any )
265  m_value = p_value instanceof ITerm ? ( (ITerm) p_value ).raw() : p_value;
266  return this;
267  }
268 }
final String functor()
returns the functor without path
Definition: CVariable.java:181
IPath subpath(final int p_fromindex)
creates a path of the start index until the end
final boolean hasVariable()
checks if the literal has variables
Definition: CVariable.java:201
final boolean m_any
boolean flag, that defines an variable which matchs always
Definition: CVariable.java:59
final boolean equals(final Object p_object)
Definition: CVariable.java:168
IVariable< T > thrownotallocated()
throws an illegal state exception iif the variable is not allocated
Definition: CVariable.java:137
boolean valueassignableto( @Nonnull final Class<?>... p_class)
checkes assignable of the value
Definition: CVariable.java:146
common structure for execution definition
IVariable< T > shallowcopy(final IPath... p_prefix)
Definition: CVariable.java:215
static IPath from( @Nonnull final String p_string)
factor method to build path
Definition: CPath.java:166
static< T > String languagestring(final T p_source, final String p_label, final Object... p_parameter)
returns the language depend string on any object
class to create a path structure
Definition: CPath.java:53
final int structurehash()
returns a hash value which defines a hash ove rthe structure
Definition: CVariable.java:249
CVariable( @Nonnull final String p_functor, @Nullable final T p_value)
ctor
Definition: CVariable.java:81
final IPath m_functor
variable / functor name
Definition: CVariable.java:55
final IPath functorpath()
returns the path of the functor
Definition: CVariable.java:188
final IPath fqnfunctor()
returns the full-qualified functor with path and name
Definition: CVariable.java:195
IVariable< T > internalset(final T p_value)
internel set for avoid any exception throwing
Definition: CVariable.java:261
final boolean any()
flag to define a "any variable"
Definition: CVariable.java:124
IVariable< T > shallowcopysuffix()
clones the object (shallow-copy) without full-qualified path, only suffix is used ...
Definition: CVariable.java:224
< T > T raw()
cast to any raw value type
static< T > T deepclone( @Nullable final T p_object)
creates a deep-clone of an object
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
CVariable( @Nonnull final String p_functor)
ctor
Definition: CVariable.java:70
IVariable< T > throwvaluenotassignableto( @Nonnull final Class<?>... p_class)
throws an illegal argument exception iif the value is not assignable to the class ...
Definition: CVariable.java:153
CVariable( @Nonnull final IPath p_functor)
ctor
Definition: CVariable.java:91
boolean mutex()
flag to check if variable has is concurrency- / thread-safe
Definition: CVariable.java:130
int size()
returns the number of path elements
boolean allocated()
returns allocated state
Definition: CVariable.java:118
public< N > N raw()
cast to any raw value type
Definition: CVariable.java:208