LightJason - AgentSpeak(L++)
CRawTerm.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;
25 
26 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
33 
34 import javax.annotation.Nonnull;
35 import javax.annotation.Nullable;
36 import java.util.Arrays;
37 import java.util.Objects;
38 
39 
45 public final class CRawTerm<T> implements IRawTerm<T>
46 {
50  public static final ITerm EMPTY = new CRawTerm<>( null );
54  private static final long serialVersionUID = 8660012856755452965L;
58  private final T m_value;
62  private final IPath m_functor;
66  private final int m_hashcode;
67 
68 
69 
76  @SuppressWarnings( "unchecked" )
77  public <N> CRawTerm( @Nullable final N p_value )
78  {
79  if ( p_value instanceof ITerm )
80  {
81  final ITerm l_term = (ITerm) p_value;
82  m_value = l_term.raw();
83  m_functor = l_term.fqnfunctor();
84  }
85  else
86  {
87  m_value = (T) p_value;
88  m_functor = Objects.isNull( p_value ) ? IPath.EMPTY : CPath.from( p_value.toString() );
89  }
90 
91  m_hashcode = Objects.isNull( m_value ) ? 0 : m_value.hashCode();
92  }
93 
94 
95 
104  public static <N> CRawTerm<N> from( final N p_value )
105  {
106  return new CRawTerm<>( p_value );
107  }
108 
109 
110  @Override
111  public final int hashCode()
112  {
113  return m_hashcode;
114  }
115 
116  @Override
117  @SuppressWarnings( "unchecked" )
118  @SuppressFBWarnings( "EQ_CHECK_FOR_OPERAND_NOT_COMPATIBLE_WITH_THIS" )
119  public final boolean equals( final Object p_object )
120  {
121  return ( Objects.nonNull( p_object ) )
122  && (
123  (
124  ( p_object instanceof IVariable<?> )
125  && ( ( (IVariable<?>) p_object ).allocated() )
126  && ( this.hashCode() == ( (IVariable<?>) p_object ).raw().hashCode() )
127  )
128  || ( ( p_object instanceof ITerm ) && ( this.hashCode() == p_object.hashCode() ) )
129  );
130  }
131 
132  @Override
133  public final String toString()
134  {
135  return Objects.isNull( m_value ) ? "" : m_value.toString();
136  }
137 
138  @Nonnull
139  @Override
140  public final String functor()
141  {
142  return m_functor.suffix();
143  }
144 
145  @Nonnull
146  @Override
147  public final IPath functorpath()
148  {
149  return m_functor.subpath( 0, m_functor.size() - 1 );
150  }
151 
152  @Nonnull
153  @Override
154  public final IPath fqnfunctor()
155  {
156  return m_functor;
157  }
158 
159  @Override
160  public final boolean hasVariable()
161  {
162  return false;
163  }
164 
165  @Override
166  @SuppressWarnings( "unchecked" )
167  public final <N> N raw()
168  {
169  return (N) m_value;
170  }
171 
172  @Override
173  public final boolean allocated()
174  {
175  return m_value != null;
176  }
177 
178  @Nonnull
179  @Override
180  public final IRawTerm<T> thrownotallocated() throws IllegalStateException
181  {
182  if ( !this.allocated() )
183  throw new CIllegalStateException( org.lightjason.agentspeak.common.CCommon.languagestring( this, "notallocated" ) );
184 
185  return this;
186  }
187 
188  @Override
189  public final boolean valueassignableto( @Nonnull final Class<?>... p_class )
190  {
191  return Arrays.stream( p_class ).anyMatch( i -> i.isAssignableFrom( m_value.getClass() ) );
192  }
193 
194  @Nonnull
195  @Override
196  public final IRawTerm<T> throwvaluenotassignableto( @Nonnull final Class<?>... p_class ) throws IllegalArgumentException
197  {
198  if ( !this.valueassignableto( p_class ) )
199  throw new CIllegalArgumentException( CCommon.languagestring( this, "notassignable", Arrays.asList( p_class ) ) );
200 
201  return this;
202  }
203 
204  @Nonnull
205  @Override
206  public final ITerm deepcopy( final IPath... p_prefix )
207  {
209  }
210 
211  @Nonnull
212  @Override
213  public final ITerm deepcopysuffix()
214  {
215  return CRawTerm.from( m_value );
216  }
217 
218  @Override
219  public final int structurehash()
220  {
221  return 0;
222  }
223 }
IPath subpath(final int p_fromindex)
creates a path of the start index until the end
final ITerm deepcopy(final IPath... p_prefix)
Definition: CRawTerm.java:206
final boolean hasVariable()
checks if the literal has variables
Definition: CRawTerm.java:160
final IPath fqnfunctor()
returns the full-qualified functor with path and name
Definition: CRawTerm.java:154
common structure for execution definition
final boolean valueassignableto( @Nonnull final Class<?>... p_class)
Definition: CRawTerm.java:189
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
final< N > N raw()
cast to any raw value type
Definition: CRawTerm.java:167
class to create a path structure
Definition: CPath.java:53
final String functor()
returns the functor without path
Definition: CRawTerm.java:140
IPath fqnfunctor()
returns the full-qualified functor with path and name
static final ITerm EMPTY
empty raw term
Definition: CRawTerm.java:50
final int m_hashcode
hash code cache
Definition: CRawTerm.java:66
final IPath functorpath()
returns the path of the functor
Definition: CRawTerm.java:147
final boolean equals(final Object p_object)
Definition: CRawTerm.java:119
final int structurehash()
returns a hash value which defines a hash ove rthe structure
Definition: CRawTerm.java:219
< T > T raw()
cast to any raw value type
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
static< T > T deepclone( @Nullable final T p_object)
creates a deep-clone of an object
String suffix()
returns the last part of the path
static final long serialVersionUID
serial id
Definition: CRawTerm.java:54
int size()
returns the number of path elements
final IRawTerm< T > throwvaluenotassignableto( @Nonnull final Class<?>... p_class)
Definition: CRawTerm.java:196
term structure for simple datatypes
Definition: CRawTerm.java:45