LightJason - AgentSpeak(L++)
CFuzzyValue.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.fuzzy;
25 
28 
29 import javax.annotation.Nonnull;
30 import javax.annotation.Nullable;
31 import java.text.MessageFormat;
32 import java.util.Arrays;
33 import java.util.Objects;
34 
35 
39 public final class CFuzzyValue<T> implements IFuzzyValue<T>
40 {
44  private final T m_value;
48  private final double m_fuzzy;
49 
55  public CFuzzyValue( @Nonnull final IFuzzyValue<T> p_value )
56  {
57  this( p_value.value(), p_value.fuzzy() );
58  }
59 
65  public CFuzzyValue( @Nonnull final T p_value )
66  {
67  this( p_value, 1 );
68  }
69 
76  public CFuzzyValue( @Nonnull final T p_value, final double p_fuzzy )
77  {
78  if ( !( ( p_fuzzy >= 0 ) && ( p_fuzzy <= 1 ) ) )
79  throw new CIllegalArgumentException( CCommon.languagestring( this, "fuzzyvalue", p_fuzzy ) );
80 
81  m_fuzzy = p_fuzzy;
82  m_value = p_value;
83  }
84 
85  @Nonnull
86  @Override
87  public final T value()
88  {
89  return m_value;
90  }
91 
92  @Override
93  public final double fuzzy()
94  {
95  return m_fuzzy;
96  }
97 
98  @Override
99  public final boolean valueassignableto( @Nonnull final Class<?>... p_class )
100  {
101  return Objects.isNull( m_value ) || Arrays.stream( p_class ).anyMatch( i -> i.isAssignableFrom( m_value.getClass() ) );
102  }
103 
104  @Nullable
105  @Override
106  public final T throwvaluenotassignableto( @Nonnull final Class<?>... p_class ) throws IllegalArgumentException
107  {
108  if ( !this.valueassignableto( p_class ) )
109  throw new CIllegalArgumentException( CCommon.languagestring( this, "notassignable", Arrays.asList( p_class ) ) );
110 
111  return m_value;
112  }
113 
114  @Override
115  public final String toString()
116  {
117  return MessageFormat.format( "{0}({1})", m_value, m_fuzzy );
118  }
119 
128  @Nonnull
129  public static <N> IFuzzyValue<N> from( @Nonnull final N p_value )
130  {
131  return new CFuzzyValue<>( p_value );
132  }
133 
143  @Nonnull
144  public static <N> IFuzzyValue<N> from( @Nonnull final N p_value, final double p_fuzzy )
145  {
146  return new CFuzzyValue<>( p_value, p_fuzzy );
147  }
148 }
static< N > IFuzzyValue< N > from( @Nonnull final N p_value, final double p_fuzzy)
factory
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
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 boolean valueassignableto( @Nonnull final Class<?>... p_class)
checkes assignable of the value
CFuzzyValue( @Nonnull final IFuzzyValue< T > p_value)
ctor
result for an immutable fuzzy value
final T throwvaluenotassignableto( @Nonnull final Class<?>... p_class)
throws an illegal argument exception iif the value is not assignable to the class ...
CFuzzyValue( @Nonnull final T p_value, final double p_fuzzy)
ctor
final double fuzzy()
returns the fuzziness