public void WithNulls()
            {
                (var cube1, var cube2) = CubePoints();

                Assert.IsTrue(CubeUtils.AreObjectsCubes(new object [] { cube1, null, cube2 }, true, out var cubeType));
                Assert.AreEqual(typeof(DoubleValue), cubeType);
            }
            public void DifferentValueTypes()
            {
                var axes1 = new Axis [] { new Axis("Axis 1", typeof(string)) };
                var axes2 = new Axis [] { new Axis("Axis 1", typeof(string)) };

                var cube1 = new Cube <DoubleValue>(new AxisSet(axes1));
                var cube2 = new Cube <LongValue>(new AxisSet(axes2));

                Assert.IsFalse(CubeUtils.AreObjectsCubes(new object [] { cube1, cube2 }, true, out var cubeType));
            }
Example #3
0
        /// <summary>
        /// Attempt to combine the supplied set of objects as a cube if possible
        /// </summary>
        /// <param name="objects">The set of objects to combine (may contain nulls)</param>
        /// <param name="combinedCube">The combined cube if appropriate</param>
        /// <param name="combinedCubeCubeValueType">The combined cube's value type if appropriate</param>
        /// <returns>True if the objects could be combined, otherwise false</returns>
        public static bool TryCombineObjectsAsCubes(IEnumerable <object> objects, out object combinedCube, out Type combinedCubeCubeValueType)
        {
            if (objects == null)
            {
                throw new ArgumentNullException(nameof(objects));
            }

            var objectsAsList = objects.Where(o => o != null).ToList();

            if (!CubeUtils.AreObjectsCubes(objectsAsList, true, out combinedCubeCubeValueType))
            {
                combinedCube = null;
                combinedCubeCubeValueType = null;
                return(false);
            }

            var convertTypeMethod = typeof(System.Linq.Enumerable).GetMethod(nameof(System.Linq.Enumerable.Cast), new Type [] { typeof(System.Collections.IEnumerable) });

            var genericCubeType   = typeof(ICube <>).MakeGenericType(combinedCubeCubeValueType);
            var castGenericMethod = convertTypeMethod.MakeGenericMethod(genericCubeType);

            var objectsAsConvertedEnumerable = castGenericMethod.Invoke(null, new object [] { objectsAsList });

            // At this point, we know that they're all of the same type, but it's still possible that they
            // couldn't be combined due to various reasons, so be safe...
            var canCombineCubesMethod        = typeof(CubeUtils).GetMethod(nameof(CanCombineCubes));
            var canCombineCubesMethodGeneric = canCombineCubesMethod.MakeGenericMethod(combinedCubeCubeValueType);
            var canCombine = (bool)canCombineCubesMethodGeneric.Invoke(null, new object [] { objectsAsConvertedEnumerable });

            if (!canCombine)
            {
                combinedCube = null;
                combinedCubeCubeValueType = null;
                return(false);
            }

            // at this point, there's no reason why we can't combine them, so time to do so
            var combineCubesMethod        = typeof(CubeUtils).GetMethod(nameof(CombineCubes));
            var combineCubesMethodGeneric = combineCubesMethod.MakeGenericMethod(combinedCubeCubeValueType);

            combinedCube = combineCubesMethodGeneric.Invoke(null, new object [] { objectsAsConvertedEnumerable, null });
            return(true);
        }
 public void OnlyNulls()
 {
     Assert.IsFalse(CubeUtils.AreObjectsCubes(new object [] { null, null }, false, out var cubeType));
 }
 public void NullSet()
 {
     Assert.ThrowsException <ArgumentNullException>(() => { CubeUtils.AreObjectsCubes(null, false, out var cubeType); });
 }
 public void EmptySet()
 {
     Assert.IsFalse(CubeUtils.AreObjectsCubes(Array.Empty <object>(), false, out var cubeType));
 }