Example #1
0
        public static object toImmutable(object obj)
        {
            if (obj == null)
            {
                return(null);
            }

            // TODO - this isn't quite right, need to clean up with FanObj.isImmutable
            if (obj is Double)
            {
                return(((Double)obj).doubleValue());
            }
            if (obj is Long)
            {
                return(((Long)obj).longValue());
            }

            if (FanObj.isImmutable(obj))
            {
                return(obj);
            }
            if (obj is List)
            {
                return(((List)obj).toImmutable());
            }
            if (obj is Map)
            {
                return(((Map)obj).toImmutable());
            }
            throw NotImmutableErr.make(FanObj.@typeof(obj).toStr()).val;
        }
Example #2
0
        //////////////////////////////////////////////////////////////////////////
        // Is/As
        //////////////////////////////////////////////////////////////////////////

        public static bool @is(object instance, Type type)
        {
            if (instance == null)
            {
                return(false);
            }
            return(FanObj.@typeof(instance).fits(type));
        }
Example #3
0
            public override void checkInCtor(object it)
            {
                if (it == m_inCtor)
                {
                    return;
                }
                string msg = it == null ? "null" : FanObj.@typeof(it).qname();

                throw ConstErr.make(msg).val;
            }
Example #4
0
 public static bool compareGT(object a, object b)
 {
     if (a == null)
     {
         return(false);
     }
     if (b == null)
     {
         return(true);
     }
     return(FanObj.compare(a, b) > 0 ? true : false);
 }
Example #5
0
 public static long compare(object a, object b)
 {
     if (a == null)
     {
         return((b == null) ? 0 : -1);
     }
     if (b == null)
     {
         return(+1);
     }
     return(FanObj.compare(a, b));
 }
Example #6
0
 public override bool isImmutable()
 {
     if (this.m_isImmutable == null)
     {
         bool isImmutable = false;
         if (m_orig.isImmutable())
         {
             isImmutable = true;
             for (int i = 0; i < m_bound.sz(); ++i)
             {
                 object obj = m_bound.get(i);
                 if (obj != null && !FanObj.isImmutable(obj))
                 {
                     isImmutable = false; break;
                 }
             }
         }
         this.m_isImmutable = Boolean.valueOf(isImmutable);
     }
     return(this.m_isImmutable.booleanValue());
 }
Example #7
0
        //////////////////////////////////////////////////////////////////////////
        // Write
        //////////////////////////////////////////////////////////////////////////

        public void writeObj(object obj)
        {
            if (obj == null)
            {
                w("null");
                return;
            }

            if (obj.GetType().FullName[0] == 'S')
            {
                if (obj is bool && (bool)obj)
                {
                    w("true");  return;
                }
                if (obj is bool && !(bool)obj)
                {
                    w("false"); return;
                }
                if (obj is double)
                {
                    FanFloat.encode((double)obj, this); return;
                }
                if (obj is long)
                {
                    FanInt.encode((long)obj, this); return;
                }
                if (obj is string)
                {
                    wStrLiteral(obj.ToString(), '"'); return;
                }
            }

            if (obj.GetType().FullName[0] == 'F')
            {
                if (obj is Boolean && (obj as Boolean).booleanValue())
                {
                    w("true"); return;
                }
                if (obj is Boolean && !(obj as Boolean).booleanValue())
                {
                    w("false"); return;
                }
                if (obj is Double)
                {
                    FanFloat.encode((obj as Double).doubleValue(), this); return;
                }
                if (obj is Long)
                {
                    FanInt.encode((obj as Long).longValue(), this); return;
                }
                if (obj is BigDecimal)
                {
                    FanDecimal.encode((BigDecimal)obj, this); return;
                }
            }

            if (obj is Literal)
            {
                ((Literal)obj).encode(this);
                return;
            }

            Type         type = FanObj.@typeof(obj);
            Serializable ser  = (Serializable)type.facet(Sys.SerializableType, false);

            if (ser != null)
            {
                if (ser.m_simple)
                {
                    writeSimple(type, obj);
                }
                else
                {
                    writeComplex(type, obj, ser);
                }
            }
            else
            {
                if (skipErrors)
                {
                    w("null /* Not serializable: ").w(type.qname()).w(" */");
                }
                else
                {
                    throw IOErr.make("Not serializable: " + type).val;
                }
            }
        }
Example #8
0
        //////////////////////////////////////////////////////////////////////////
        // Complex
        //////////////////////////////////////////////////////////////////////////

        private void writeComplex(Type type, object obj, Serializable ser)
        {
            wType(type);

            bool   first  = true;
            object defObj = null;

            if (skipDefaults)
            {
                defObj = FanObj.@typeof(obj).make();
            }

            List fields = type.fields();

            for (int i = 0; i < fields.sz(); ++i)
            {
                Field f = (Field)fields.get(i);

                // skip static, transient, and synthetic (once) fields
                if (f.isStatic() || f.isSynthetic() || f.hasFacet(Sys.TransientType))
                {
                    continue;
                }

                // get the value
                object val = f.get(obj);

                // if skipping defaults
                if (defObj != null)
                {
                    object defVal = f.get(defObj);
                    if (OpUtil.compareEQ(val, defVal))
                    {
                        continue;
                    }
                }

                // if first then open braces
                if (first)
                {
                    w('\n').wIndent().w('{').w('\n'); level++; first = false;
                }

                // field name =
                wIndent().w(f.name()).w('=');

                // field value
                curFieldType = f.type().toNonNullable();
                writeObj(val);
                curFieldType = null;

                w('\n');
            }

            // if collection
            if (ser.m_collection)
            {
                first = writeCollectionItems(type, obj, first);
            }

            // if we output fields, then close braces
            if (!first)
            {
                level--; wIndent().w('}');
            }
        }
Example #9
0
        //////////////////////////////////////////////////////////////////////////
        // Simple
        //////////////////////////////////////////////////////////////////////////

        private void writeSimple(Type type, object obj)
        {
            wType(type).w('(').wStrLiteral(FanObj.toStr(obj), '"').w(')');
        }