http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang/Types/INode.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang/Types/INode.cs b/lang/cs/Org.Apache.REEF.Tang/Types/INode.cs new file mode 100644 index 0000000..aade8bb --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang/Types/INode.cs @@ -0,0 +1,37 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +using System; + +namespace Org.Apache.REEF.Tang.Types +{ + public interface INode : IComparable<INode>, ITraversable<INode> + { + string GetName(); + + string GetFullName(); + + bool Contains(string key); + + INode Get(string key); + + INode GetParent(); + + void Add(INode node); + } +}
http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang/Types/IPackageNode.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang/Types/IPackageNode.cs b/lang/cs/Org.Apache.REEF.Tang/Types/IPackageNode.cs new file mode 100644 index 0000000..873f511 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang/Types/IPackageNode.cs @@ -0,0 +1,24 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +namespace Org.Apache.REEF.Tang.Types +{ + public interface IPackageNode : INode + { + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang/Types/ITraversable.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang/Types/ITraversable.cs b/lang/cs/Org.Apache.REEF.Tang/Types/ITraversable.cs new file mode 100644 index 0000000..eed28e9 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang/Types/ITraversable.cs @@ -0,0 +1,27 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +using System.Collections.Generic; + +namespace Org.Apache.REEF.Tang.Types +{ + public interface ITraversable<T> where T : ITraversable<T> + { + ICollection<T> GetChildren(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang/Util/AbstractMonotonicMultiMap.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang/Util/AbstractMonotonicMultiMap.cs b/lang/cs/Org.Apache.REEF.Tang/Util/AbstractMonotonicMultiMap.cs new file mode 100644 index 0000000..22cb4a6 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang/Util/AbstractMonotonicMultiMap.cs @@ -0,0 +1,263 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using Org.Apache.REEF.Utilities.Logging; + +namespace Org.Apache.REEF.Tang.Util +{ + public abstract class AbstractMonotonicMultiMap<K, V> : ICollection<KeyValuePair<K, V>> + { + private static readonly Logger LOGGER = Logger.GetLogger(typeof(AbstractMonotonicMultiMap<K, V>)); + + private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); + + private IDictionary<K, ISet<V>> map; + + private int size = 0; + + public AbstractMonotonicMultiMap(IDictionary<K, ISet<V>> map) + { + this.map = map; + } + + public ICollection<K> Keys + { + get { return map.Keys; } + } + + public int Count + { + get { return size; } + } + + public bool IsReadOnly + { + get { throw new NotImplementedException(); } + } + + public void Add(K key, V val) + { + _lock.EnterWriteLock(); + try + { + ISet<V> vals; + map.TryGetValue(key, out vals); + + if (vals == null) + { + vals = new MonotonicHashSet<V>(); + map.Add(key, vals); + } + vals.Add(val); + size++; + } + finally + { + if (_lock.IsWriteLockHeld) + { + _lock.ExitWriteLock(); + } + } + } + + public ISet<V> GetValuesForKey(K key) + { + _lock.EnterReadLock(); + try + { + ISet<V> ret; + map.TryGetValue(key, out ret); + if (ret == null) + { + return new MonotonicHashSet<V>(); + } + return ret; + } + finally + { + if (_lock.IsReadLockHeld) + { + _lock.ExitReadLock(); + } + } + } + + public bool Contains(K key, V val) + { + _lock.EnterReadLock(); + try + { + ISet<V> vals; + map.TryGetValue(key, out vals); + + if (vals != null) + { + return vals.Contains(val); + } + return false; + } + finally + { + if (_lock.IsReadLockHeld) + { + _lock.ExitReadLock(); + } + } + } + + public bool Add(KeyValuePair<K, V> e) + { + Add(e.Key, e.Value); + return true; + } + + public bool AddAll(ICollection<KeyValuePair<K, V>> c) // where T : KeyValuePair<K, V> + { + bool ret = false; + foreach (KeyValuePair<K, V> e in c) + { + Add(e); + ret = true; + } + return ret; + } + + public void Clear() + { + throw new NotSupportedException("MonotonicMultiMap cannot be cleared!"); + } + + public bool Contains(object o) + { + KeyValuePair<K, V> e = (KeyValuePair<K, V>)o; + return Contains((K)e.Key, (V)e.Value); + } + + public bool ContainsAll<T>(ICollection<T> c) + { + foreach (object o in c) + { + if (!Contains(o)) + { + return false; + } + } + return true; + } + + public bool IsEmpty() + { + return size == 0; + } + + public ISet<V> Values() + { + _lock.EnterReadLock(); + + try + { + ISet<V> s = new HashSet<V>(); + foreach (KeyValuePair<K, V> e in this) + { + s.Add(e.Value); + } + return s; + } + finally + { + if (_lock.IsReadLockHeld) + { + _lock.ExitReadLock(); + } + } + } + + public int Size() + { + return size; + } + + public bool ContainsKey(K k) + { + _lock.EnterReadLock(); + + try + { + if (map.ContainsKey(k)) + { + return (GetValuesForKey(k).Count != 0); + } + return false; + } + finally + { + if (_lock.IsReadLockHeld) + { + _lock.ExitReadLock(); + } + } + } + + void ICollection<KeyValuePair<K, V>>.Add(KeyValuePair<K, V> item) + { + throw new NotImplementedException(); + } + + public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex) + { + throw new NotImplementedException(); + } + + public bool Remove(KeyValuePair<K, V> item) + { + throw new NotImplementedException(); + } + + public bool Contains(KeyValuePair<K, V> item) + { + throw new NotImplementedException(); + } + + public IEnumerator<KeyValuePair<K, V>> GetEnumerator() + { + _lock.EnterReadLock(); + + try + { + return map.SelectMany(kvp => kvp.Value, (kvp, v) => new KeyValuePair<K, V>(kvp.Key, v)).GetEnumerator(); + } + finally + { + if (_lock.IsReadLockHeld) + { + _lock.ExitReadLock(); + } + } + } + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang/Util/AssemblyLoader.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang/Util/AssemblyLoader.cs b/lang/cs/Org.Apache.REEF.Tang/Util/AssemblyLoader.cs new file mode 100644 index 0000000..f5e30f9 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang/Util/AssemblyLoader.cs @@ -0,0 +1,70 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +using System; +using System.Collections.Generic; +using System.Reflection; +using Org.Apache.REEF.Utilities.Logging; + +namespace Org.Apache.REEF.Tang.Util +{ + public class AssemblyLoader + { + private static readonly Logger LOGGER = Logger.GetLogger(typeof(AssemblyLoader)); + + public IList<Assembly> Assemblies { get; set; } + + public AssemblyLoader(string[] files) + { + Assemblies = new List<Assembly>(); + foreach (var a in files) + { + Assemblies.Add(Assembly.Load(a)); + } + } + + public Type GetType(string name) + { + Type t = Type.GetType(name); + if (t == null) + { + foreach (var a in Assemblies) + { + t = a.GetType(name); + if (t != null) + { + return t; + } + } + + foreach (var a in AppDomain.CurrentDomain.GetAssemblies()) + { + t = a.GetType(name); + if (t != null) + break; + } + } + + if (t == null) + { + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new ApplicationException("Not able to get Type from the name provided: " + name), LOGGER); + } + return t; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang/Util/GenericType.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang/Util/GenericType.cs b/lang/cs/Org.Apache.REEF.Tang/Util/GenericType.cs new file mode 100644 index 0000000..361ffdf --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang/Util/GenericType.cs @@ -0,0 +1,49 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +using System; + +namespace Org.Apache.REEF.Tang.Util +{ + public class GenericType<T> + { + public static readonly GenericType<T> Class = null; + + public Type TypeT + { + get { return typeof(T); } + } + + public override bool Equals(object obj) + { + GenericType<T> other = obj as GenericType<T>; + if (other == null) + { + return false; + } + + return TypeT == other.TypeT; + } + + public override int GetHashCode() + { + return TypeT.GetHashCode(); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicHashMap.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicHashMap.cs b/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicHashMap.cs new file mode 100644 index 0000000..7c5d4c6 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicHashMap.cs @@ -0,0 +1,83 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +using System; +using System.Collections.Generic; +using Org.Apache.REEF.Utilities.Logging; + +namespace Org.Apache.REEF.Tang.Util +{ + public class MonotonicHashMap<T, U> : Dictionary<T, U> + { + private static readonly Logger LOGGER = Logger.GetLogger(typeof(MonotonicHashMap<T, U>)); + + public new void Add(T key, U value) + { + U old; + TryGetValue(key, out old); + if (old != null) + { + var ex = new ArgumentException("Attempt to re-add: [" + key + "] old value: " + old + " new value " + value); + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); + } + base.Add(key, value); + } + + public void AddAll(IDictionary<T, U> m) + { + foreach (T t in m.Keys) + { + if (ContainsKey(t)) + { + U old; + m.TryGetValue(t, out old); + Add(t, old); // guaranteed to throw. + } + } + foreach (T t in m.Keys) + { + U old; + m.TryGetValue(t, out old); + Add(t, old); + } + } + + public bool IsEmpty() + { + return Count == 0; + } + + public U Get(T key) + { + U val; + TryGetValue(key, out val); + return val; + } + + public new void Clear() + { + throw new NotSupportedException(); + } + + public new bool Remove(T key) + { + throw new NotSupportedException(); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicHashSet.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicHashSet.cs b/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicHashSet.cs new file mode 100644 index 0000000..3239719 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicHashSet.cs @@ -0,0 +1,131 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using Org.Apache.REEF.Utilities.Logging; + +namespace Org.Apache.REEF.Tang.Util +{ + public class MonotonicHashSet<T> : HashSet<T> + { + private static readonly Logger LOGGER = Logger.GetLogger(typeof(MonotonicHashSet<T>)); + private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); + + public MonotonicHashSet() + { + } + + public MonotonicHashSet(IEnumerable<T> collection) + : base(collection) + { + } + + public MonotonicHashSet(T[] collection) + : base(collection.ToList()) + { + } + + public new bool Add(T e) + { + _lock.EnterWriteLock(); + + try + { + if (Contains(e)) + { + var ex = new ArgumentException("Attempt to re-add " + e + + " to MonotonicSet!"); + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); + } + return base.Add(e); + } + finally + { + if (_lock.IsWriteLockHeld) + { + _lock.ExitWriteLock(); + } + } + } + + public bool AddAll(ICollection<T> c) + { + _lock.EnterWriteLock(); + try + { + foreach (T t in c) + { + if (Contains(t)) + { + var ex = new ArgumentException("Attempt to re-add " + t + + " to MonotonicSet!"); + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); + } + base.Add(t); + } + } + finally + { + if (_lock.IsWriteLockHeld) + { + _lock.ExitWriteLock(); + } + } + return c.Count != 0; + } + + public bool ContainsAll(ICollection<T> c) + { + _lock.EnterReadLock(); + + try + { + foreach (T t in c) + { + if (!Contains(t)) + { + return false; + } + } + } + finally + { + if (_lock.IsReadLockHeld) + { + _lock.ExitReadLock(); + } + } + return true; + } + + public new void Clear() + { + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new NotSupportedException("Attempt to clear MonotonicSet!"), LOGGER); + } + + public bool Remove(object o) + { + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new NotSupportedException("Attempt to remove " + o + " from MonotonicSet!"), LOGGER); + return false; + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicMultiHashMap.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicMultiHashMap.cs b/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicMultiHashMap.cs new file mode 100644 index 0000000..fca5ffd --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicMultiHashMap.cs @@ -0,0 +1,30 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +using System.Collections.Generic; + +namespace Org.Apache.REEF.Tang.Util +{ + public class MonotonicMultiHashMap<K, V> : AbstractMonotonicMultiMap<K, V> + { + public MonotonicMultiHashMap() : base(new MonotonicHashMap<K, ISet<V>>()) + { + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicMultiMap.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicMultiMap.cs b/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicMultiMap.cs new file mode 100644 index 0000000..38b2e5c --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicMultiMap.cs @@ -0,0 +1,30 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +using System.Collections.Generic; + +namespace Org.Apache.REEF.Tang.Util +{ + public class MonotonicMultiMap<K, V> : AbstractMonotonicMultiMap<K, V> + { + public MonotonicMultiMap() : base(new MonotonicTreeMap<K, ISet<V>>()) + { + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicSet.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicSet.cs b/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicSet.cs new file mode 100644 index 0000000..e8831ff --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicSet.cs @@ -0,0 +1,106 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +using System; +using System.Collections.Generic; +using Org.Apache.REEF.Utilities.Logging; + +namespace Org.Apache.REEF.Tang.Util +{ + public class MonotonicSet<T> : SortedSet<T> + { + private static readonly Logger LOGGER = Logger.GetLogger(typeof(MonotonicSet<T>)); + + //private static readonly long serialVersionUID = 1L; + public MonotonicSet() : base() + { + } + + //public MonotonicSet(SortedSet<T> c) : base(c.Comparer) + //{ + // AddAll(c); + //} + + public MonotonicSet(ICollection<T> c) + : base(c) + { + } + + public MonotonicSet(IComparer<T> c) + : base(c) + { + } + + public new bool Add(T e) //TODO + { + if (this.Contains(e)) + { + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new ArgumentException("Attempt to re-add " + e + + " to MonotonicSet!"), LOGGER); + } + return base.Add(e); + } + + public bool AddAll(ICollection<T> c) + { + foreach (T t in c) + { + this.Add(t); + } + return c.Count != 0; + } + + public bool ContainsAll(ICollection<T> c) + { + foreach (T t in c) + { + if (!this.Contains(t)) + { + return false; + } + } + return true; + } + + public bool AddAllIgnoreDuplicates(ICollection<T> c) + { + bool ret = false; + foreach (T t in c) + { + if (!Contains(t)) + { + Add(t); + ret = true; + } + } + return ret; + } + + public override void Clear() + { + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new NotSupportedException("Attempt to clear MonotonicSet!"), LOGGER); + } + + public bool Remove(Object o) + { + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new NotSupportedException("Attempt to remove " + o + + " from MonotonicSet!"), LOGGER); + return false; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicTreeMap.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicTreeMap.cs b/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicTreeMap.cs new file mode 100644 index 0000000..328dfb7 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang/Util/MonotonicTreeMap.cs @@ -0,0 +1,68 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +using System; +using System.Collections.Generic; +using System.Threading; +using Org.Apache.REEF.Utilities.Logging; + +namespace Org.Apache.REEF.Tang.Util +{ + public class MonotonicTreeMap<TKey, TVal> : SortedDictionary<TKey, TVal> + { + private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); + + private static readonly Logger LOGGER = Logger.GetLogger(typeof(MonotonicTreeMap<TKey, TVal>)); + + public new void Add(TKey key, TVal value) + { + _lock.EnterWriteLock(); + try + { + TVal val; + if (base.TryGetValue(key, out val)) + { + var ex = new ArgumentException("Attempt to re-add: [" + key + + "]\n old value: " + val + " new value " + value); + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); + } + else + { + base.Add(key, value); + } + } + finally + { + if (_lock.IsWriteLockHeld) + { + _lock.ExitWriteLock(); + } + } + } + + public new void Clear() //TODO + { + throw new NotSupportedException(); + } + + public new void Remove(TKey key) //TODO + { + throw new NotSupportedException(); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang/Util/ReflectionUtilities.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang/Util/ReflectionUtilities.cs b/lang/cs/Org.Apache.REEF.Tang/Util/ReflectionUtilities.cs new file mode 100644 index 0000000..bd50e77 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang/Util/ReflectionUtilities.cs @@ -0,0 +1,594 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Reflection; +using System.Runtime.CompilerServices; +using Org.Apache.REEF.Utilities.Logging; +using Org.Apache.REEF.Tang.Annotations; +using Org.Apache.REEF.Tang.Exceptions; + +namespace Org.Apache.REEF.Tang.Util +{ + public class ReflectionUtilities + { + private static readonly Logger LOGGER = Logger.GetLogger(typeof(ReflectionUtilities)); + + public static readonly string Regexp = "[\\.\\+]"; + + /// <summary> + /// Gets the AssemblyQualifiedName from the Type. This name is used in ClassHierarchy + /// as a key when add a node as a child to parent. THe name is used as FullName in a Node + /// </summary> + /// <param name="name">The name.</param> + /// <returns></returns> + /// <exception cref="System.ArgumentException">null is passed in FullName() in ReflectionUtilities</exception> + public static string GetAssemblyQualifiedName(Type name) + { + if (name == null) + { + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new ArgumentException("null is passed in FullName() in ReflectionUtilities"), LOGGER); + } + + Type t = EnsureInterfaceType(name); + + if (t.AssemblyQualifiedName == null && t.Name == null && t.FullName == null) + { + LOGGER.Log(Level.Warning, "The type's name is null: " + t.ToString()); + } + + if (t.AssemblyQualifiedName == null && t.Name != null) + { + return t.Name; + } + + return t.AssemblyQualifiedName; + } + + /// <summary> + /// It returns Type.FullName. This name is used as Name in a Node. + /// It is not unique for a generic type with different type of arguments. + /// It is used for toString or debug info as AssemblyQualifiedName is really long + /// </summary> + /// <param name="name">The name.</param> + /// <returns></returns> + public static string GetName(Type name) + { + if (name.FullName != null) + { + return name.FullName; + } + + //The following lines should be not reached by C# syntax definition. However, it happens for some generic type such as AbstractObserver<T> + //It results in name as null. When null name in the class node gets deserialzed, as name is required filed in class hierarchy proto buffer schame, + //it causes exception during deserialization. The code below is to use first portion of AssemblyQualifiedName for the name of the node node in case type.name is null. + string[] parts = GetAssemblyQualifiedName(name).Split(','); + return parts[0]; + } + + /// <summary> + /// Gets the interface target. + // Foo<T> , given Foo<T> and Foo return T + // example class Foo : Bar<U>, Bas<T> + // iface: Bar, type: Foo, return U + // iface: Bas, type: Foo, return T + // class ACons implements IExternalConstructor<A> + // iface: IExternalConstructor<>, type: ACons return A + /// </summary> + /// <param name="iface">The iface.</param> + /// <param name="type">The type.</param> + /// <returns></returns> + public static Type GetInterfaceTarget(Type iface, Type type) + { + foreach (Type t in ReflectionUtilities.ClassAndAncestors(type)) + { + if (IsGenericTypeof(iface, t)) + { + return t.GetGenericArguments()[0]; //verify it + } + } + return null; + } + + public static Type GetInterfaceTargetForType(Type iface, Type type) + { + if (IsGenericTypeof(iface, type)) + { + return type.GetGenericArguments()[0]; //verify it + } + return null; + } + + /// <summary> + /// Determines whether [is generic typeof] [the specified iface]. + /// </summary> + /// <param name="iface">The iface.</param> + /// <param name="type">The type.</param> + /// <returns> + /// <c>true</c> if [is generic typeof] [the specified iface]; otherwise, <c>false</c>. + /// </returns> + /// <exception cref="System.ApplicationException"></exception> + public static bool IsGenericTypeof(Type iface, Type type) + { + if (iface == null || type == null) + { + var ex = new ApplicationException(string.Format(CultureInfo.CurrentCulture, + "The type passed in IsGenericTypeof is null: iface : {0} type: {1}. ", + iface, type)); + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); + } + if (type.IsGenericType) + { + if (GetAssemblyQualifiedName(iface).Equals(GetAssemblyQualifiedNameForGeneric(type))) + { + return true; + } + } + return false; + } + + /// <summary> + /// Classes the and ancestors. + /// </summary> + /// <param name="c">The c.</param> + /// <returns></returns> + public static IEnumerable<Type> ClassAndAncestors(Type c) + { + List<Type> workQueue = new List<Type>(); + workQueue.Add(c); //including itself + + foreach (Type t in c.GetInterfaces()) + { + workQueue.Add(t); + } + + Type b = c.BaseType; + while (b != null) + { + workQueue.Add(b); + b = b.BaseType; + } + + if (c.IsInterface) + { + workQueue.Add(typeof (object)); + } + + return workQueue; + } + + public static IEnumerable<Type> ClassAndAncestorsExcludeSelf(Type c) + { + List<Type> workQueue = new List<Type>(); + //workQueue.Add(c); //including itself + + foreach (Type t in c.GetInterfaces()) + { + workQueue.Add(t); + } + + Type b = c.BaseType; + while (b != null) + { + workQueue.Add(b); + b = b.BaseType; + } + + if (c.IsInterface) + { + workQueue.Add(typeof(object)); + } + + return workQueue; + } + + /// <summary> + /// Boxes the class. + /// </summary> + /// <param name="c">The c.</param> + /// <returns></returns> + /// <exception cref="System.NotSupportedException">Encountered unknown primitive type!</exception> + public static Type BoxClass(Type c) + { + if (c.IsPrimitive && c != typeof (Type)) + { + if (c == typeof (bool)) + { + return typeof (Boolean); + } + else if (c == typeof (byte)) + { + return typeof (Byte); + } + else if (c == typeof (char)) + { + return typeof (Char); + } + else if (c == typeof (short)) + { + return typeof (Int16); + } + else if (c == typeof (int)) + { + return typeof (Int32); + } + else if (c == typeof (long)) + { + return typeof (Int64); + } + else if (c == typeof (float)) + { + return typeof (Single); + } + else if (c == typeof (double)) + { + return typeof (Double); + } + else + { + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new NotSupportedException( + "Encountered unknown primitive type!"), LOGGER); + return c; + } + } + else + { + return c; + } + } + + /// <summary> + /// Determines whether the specified to is coercable. + // public static boolean isCoercable(Class<?> to, Class<?> from) castable from to both are numbers and from has a few bits or subclass relationship + /// </summary> + /// <param name="to">To.</param> + /// <param name="from">From.</param> + /// <returns> + /// <c>true</c> if the specified to is coercable; otherwise, <c>false</c>. + /// </returns> + public static bool IsCoercable(Type to, Type from) + { + to = BoxClass(to); + from = BoxClass(from); + //TODO + //if (Number.class.isAssignableFrom(to) + // && Number.class.isAssignableFrom(from)) { + //return sizeof.get(from) <= sizeof.get(to); + return to.IsAssignableFrom(from); + //return IsAssignableFromIgnoreGeneric(to, from); + } + + /// <summary> + /// Determines whether [is assignable from ignore generic] [the specified to]. + /// </summary> + /// <param name="to">To.</param> + /// <param name="from">From.</param> + /// <returns> + /// <c>true</c> if [is assignable from ignore generic] [the specified to]; otherwise, <c>false</c>. + /// </returns> + public static bool IsAssignableFromIgnoreGeneric(Type to, Type from) + { + var f = ClassAndAncestors(from); + foreach (Type t in f) + { + if (GetAssemblyQualifiedName(to).Equals(GetAssemblyQualifiedNameForGeneric(t))) + { + return true; + } + } + return false; + } + + /// <summary> + /// Ensures the type of the interface. For generic types, full name could be null. In this case, we need to + /// get GetGenericTypeDefinition for the type so that to rerain all teh type information + /// </summary> + /// <param name="interf">The interf.</param> + /// <returns></returns> + public static Type EnsureInterfaceType(Type interf) + { + if (interf != null && interf.IsGenericType && null == interf.FullName) + { + return interf.GetGenericTypeDefinition(); //this is to test if this line is ever reached + } + return interf; + } + + /// <summary> + /// Gets the assembly qualified name for generic. + /// </summary> + /// <param name="t">The t.</param> + /// <returns></returns> + public static string GetAssemblyQualifiedNameForGeneric(Type t) + { + Type t1 = t; + if (t.IsGenericType) + { + t1 = t.GetGenericTypeDefinition(); + } + return t1.AssemblyQualifiedName; + } + + /// <summary> + /// Determines whether [is instance of generic] [the specified p]. + /// </summary> + /// <param name="p">The p.</param> + /// <param name="t">The t.</param> + /// <returns> + /// <c>true</c> if [is instance of generic] [the specified p]; otherwise, <c>false</c>. + /// </returns> + public static bool IsInstanceOfGeneric(object p, Type t) + { + foreach (var g in ReflectionUtilities.ClassAndAncestors(p.GetType())) + { + if (GetAssemblyQualifiedNameForGeneric(t).Equals(GetAssemblyQualifiedNameForGeneric(g))) + { + return true; + } + } + return false; + } + + /// <summary> + /// Gets the name of the type by. + /// </summary> + /// <param name="name">The name.</param> + /// <returns></returns> + /// <exception cref="System.ApplicationException">Not able to get Type from the name provided: + name</exception> + public static Type GetTypeByName(string name) + { + Type t = null; + + t = Type.GetType(name); + + if (t == null) + { + foreach (var a in AppDomain.CurrentDomain.GetAssemblies()) + { + t = a.GetType(name); + if (t != null) + break; + } + } + if (t == null) + { + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new ApplicationException("Not able to get Type from the name provided: " + name), LOGGER); + } + + return t; + } + + /// <summary> + /// Gets the enclosing classes. + /// </summary> + /// <param name="t">The t.</param> + /// <returns></returns> + public static Type[] GetEnclosingClasses(Type t) + { + IList<Type> l = new List<Type>(); + l.Add(t); + Type current = t.DeclaringType; + while (current != null) + { + l.Add(current); + current = current.DeclaringType; + } + return l.Reverse().ToArray(); + } + + /// <summary> + /// Gets the enclosing class names. + /// </summary> + /// <param name="t">The t.</param> + /// <returns></returns> + /// <exception cref="System.ApplicationException">The Type passed to GetEnclosingClassShortNames is null</exception> + public static string[] GetEnclosingClassNames(Type t) + { + if (t == null) + { + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new ApplicationException("The Type passed to GetEnclosingClassShortNames is null"), LOGGER); + } + Type[] ts = GetEnclosingClasses(t); + string[] result = new string[ts.Length]; + for (int i = 0; i < ts.Length; i++) + { + result[i] = GetAssemblyQualifiedName(ts[i]); + } + + return result; + } + + /// <summary> + /// Gets the enclosing class names. + /// </summary> + /// <param name="fullName">The full name.</param> + /// <returns></returns> + /// <exception cref="System.ApplicationException">The name passed to GetEnclosingClassShortNames is null</exception> + public static string[] GetEnclosingClassNames(string fullName) + { + if (fullName == null) + { + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new ApplicationException("The name passed to GetEnclosingClassShortNames is null"), LOGGER); + } + Type t = ReflectionUtilities.GetTypeByName(fullName); + return GetEnclosingClassNames(t); + } + + /// <summary> + /// Gets the named parameter target or null. + /// </summary> + /// <param name="type">The type.</param> + /// <returns></returns> + /// <exception cref="ClassHierarchyException">Named parameter + GetName(type) + implements + /// + multiple interfaces. It is only allowed to implement Name</exception> + public static Type GetNamedParameterTargetOrNull(Type type) + { + var npAnnotation = type.GetCustomAttribute<NamedParameterAttribute>(); + if (npAnnotation != null) + { + Type[] intfs = type.GetInterfaces(); + if (intfs.Length > 1) + { + var ex = new ClassHierarchyException("Named parameter " + GetName(type) + " implements " + + "multiple interfaces. It is only allowed to implement Name<T>"); + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); + + } + else if (intfs.Length == 0 || !IsName(intfs[0])) + { + var ex = new ClassHierarchyException("Found illegal [NamedParameter " + GetName(type) + + " does not implement Name<T>"); + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); + } + Type[] args = intfs[0].GetGenericArguments(); + if (args.Length > 1) + { + var ex = new ClassHierarchyException("Found illegal [NamedParameter " + GetName(type) + + " that has more than one arguments"); + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); + } + if (args.Length == 0) + { + var ex = new ClassHierarchyException("Found illegal [NamedParameter " + GetName(type) + + " that has no argument"); + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); + } + if (HasConstructor(type) || HasInjectableConstructor(type)) + { + var ex = new ClassHierarchyException("Named parameter " + GetName(type) + " has " + + (HasInjectableConstructor(type) ? "an injectable" : "a") + " constructor. " + + " Named parameters must not declare any constructors."); + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); + } + + return args[0]; + } + + if (ImplementName(type)) //Implement Name<> but no [NamedParameter] attribute + { + var ex = new ClassHierarchyException("Named parameter " + GetName(type) + + " is missing its [NamedParameter] attribute."); + Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); + } + return null; + } + + public static IEnumerable<Type> GetInterfaces(Type type, bool includeInherited) + { + if (includeInherited || type.BaseType == null) + return type.GetInterfaces(); + else + return type.GetInterfaces().Except(type.BaseType.GetInterfaces()); + } + + // Here is a more elaborate hack to test for annonymous type: + // http://stackoverflow.com/questions/2483023/how-to-test-if-a-type-is-anonymous + // compiler generated classes are always recreatable and need not additional references to check for. + public static bool IsAnnonymousType(Type type) + { + if (type != null) + { + // HACK: The only way to detect anonymous types right now. + return Attribute.IsDefined(type, typeof(CompilerGeneratedAttribute), false) + && type.IsGenericType && type.Name.Contains("AnonymousType") + && (type.Name.StartsWith("<>", true, CultureInfo.CurrentCulture) || type.Name.StartsWith("VB$", true, CultureInfo.CurrentCulture)) + && (type.Attributes & TypeAttributes.NotPublic) == TypeAttributes.NotPublic; + } + return false; + } + + private static bool ImplementName(Type type) + { + foreach (Type t in type.GetInterfaces()) + { + if (IsName(t)) + { + return true; + } + } + return false; + } + + private static bool IsName(Type t) + { + if (t.IsGenericType) + { + return t.GetGenericTypeDefinition().AssemblyQualifiedName.Equals(typeof (Name<>).AssemblyQualifiedName); + } + return false; + } + + private static bool HasConstructor(Type type) + { + bool hasConstructor = false; + + ConstructorInfo[] constructors = + type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + + if (constructors.Length > 1) + { + hasConstructor = true; + } + + if (constructors.Length == 1) + { + ConstructorInfo c = constructors[0]; + ParameterInfo[] p = c.GetParameters(); + if (p.Length > 1) + { + // Multiple args. Definitely not implicit. + hasConstructor = true; + } + else if (p.Length == 1) + { + // One arg. Could be an inner class, in which case the compiler + // included an implicit one parameter constructor that takes the + // enclosing type. + if (p[0].ParameterType != type.DeclaringType) + { + hasConstructor = true; + } + } + } + return hasConstructor; + } + + private static bool HasInjectableConstructor(Type type) + { + bool isInjectable = false; + + ConstructorInfo[] constructors = + type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + + foreach (ConstructorInfo c in constructors) + { + foreach (Attribute a in c.GetCustomAttributes()) + { + if (a is InjectAttribute) + { + isInjectable = true; + } + } + } + return isInjectable; + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang/Util/SetValuedKey.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang/Util/SetValuedKey.cs b/lang/cs/Org.Apache.REEF.Tang/Util/SetValuedKey.cs new file mode 100644 index 0000000..5d146cb --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang/Util/SetValuedKey.cs @@ -0,0 +1,62 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Org.Apache.REEF.Tang.Util +{ + class SetValuedKey + { + public IList<object> key; + + public SetValuedKey(object[] ts, object[] us) + { + key = ts.ToList<object>(); + foreach (var o in us) + { + key.Add(o); + } + } + + public override int GetHashCode() + { + int i = 0; + foreach (object t in key) + { + i += t.GetHashCode(); + } + return i; + } + + public override bool Equals(Object o) + { + SetValuedKey other = (SetValuedKey)o; + if (other.key.Count != this.key.Count) { return false; } + for (int i = 0; i < this.key.Count; i++) + { + if (this.key[i].Equals(other.key[i])) + { + return false; + } + } + return true; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang/packages.config ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang/packages.config b/lang/cs/Org.Apache.REEF.Tang/packages.config new file mode 100644 index 0000000..933b7e1 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang/packages.config @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> +<packages> + <package id="Microsoft.Hadoop.Avro" version="1.4.0.0" targetFramework="net45" /> + <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" /> + <package id="protobuf-net" version="2.0.0.668" targetFramework="net45" /> +</packages> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Utilities/AvroUtils.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Utilities/AvroUtils.cs b/lang/cs/Org.Apache.REEF.Utilities/AvroUtils.cs new file mode 100644 index 0000000..7a92b49 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Utilities/AvroUtils.cs @@ -0,0 +1,61 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Hadoop.Avro; + +namespace Org.Apache.REEF.Utilities +{ + public class AvroUtils + { + /// <summary> + /// Convert an object to byte array using Avro serializiation + /// </summary> + /// <param name="obj">The object to serialize</param> + /// <returns>The serialized object in a byte array</returns> + public static byte[] AvroSerialize<T>(T obj) + { + IAvroSerializer<T> serializer = AvroSerializer.Create<T>(); + using (MemoryStream stream = new MemoryStream()) + { + serializer.Serialize(stream, obj); + return stream.GetBuffer(); + } + } + + /// <summary> + /// Converts a byte array to an object using Avro deserialization. + /// </summary> + /// <param name="data">The byte array to deserialize</param> + /// <returns>The deserialized object</returns> + public static T AvroDeserialize<T>(byte[] data) + { + IAvroSerializer<T> deserializer = AvroSerializer.Create<T>(); + using (MemoryStream stream = new MemoryStream(data)) + { + return deserializer.Deserialize(stream); + } + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Utilities/ByteUtilities.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Utilities/ByteUtilities.cs b/lang/cs/Org.Apache.REEF.Utilities/ByteUtilities.cs new file mode 100644 index 0000000..e7c71f4 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Utilities/ByteUtilities.cs @@ -0,0 +1,45 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +using System; +using System.Text; + +namespace Org.Apache.REEF.Utilities +{ + public class ByteUtilities + { + public static byte[] StringToByteArrays(string s) + { + return Encoding.UTF8.GetBytes(s); + } + + public static string ByteArrarysToString(byte[] b) + { + return Encoding.UTF8.GetString(b); + } + + public static byte[] CopyBytesFrom(byte[] from) + { + int length = Buffer.ByteLength(from); + byte[] to = new byte[length]; + Buffer.BlockCopy(from, 0, to, 0, length); + return to; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Utilities/Diagnostics/DiagnosticsMessages.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Utilities/Diagnostics/DiagnosticsMessages.cs b/lang/cs/Org.Apache.REEF.Utilities/Diagnostics/DiagnosticsMessages.cs new file mode 100644 index 0000000..165c54c --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Utilities/Diagnostics/DiagnosticsMessages.cs @@ -0,0 +1,42 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Org.Apache.REEF.Utilities.Diagnostics +{ + public class DiagnosticsMessages + { + public const string ExceptionThrowing = "ExceptionThrowing"; + + public const string ExceptionCaught = "ExceptionCaught"; + + public const string DumperTimeout = "DumperTimeout"; + + public const string DumperError = "DumperError"; + + public const string CallingDumper = "CallingDumper"; + + public const string DumperException = "DumperException"; + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Utilities/Diagnostics/Exceptions.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Utilities/Diagnostics/Exceptions.cs b/lang/cs/Org.Apache.REEF.Utilities/Diagnostics/Exceptions.cs new file mode 100644 index 0000000..035fdbe --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Utilities/Diagnostics/Exceptions.cs @@ -0,0 +1,282 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +using System; +using System.Globalization; +using System.Text; +using Org.Apache.REEF.Utilities.Logging; + +namespace Org.Apache.REEF.Utilities.Diagnostics +{ + public static class Exceptions + { + #region methods + /// <summary> + /// Call this method to throw an exception. + /// </summary> + /// <remarks> + /// Calling this method will trace the exception and do other common processing, + /// and then it will throw the exception. This method traces the exception type + /// and message at error level and the full stack trace at all other levels. + /// </remarks> + /// <example> + /// Exceptions.Throw(new Exception("Some exception")); + /// </example> + /// <param name="exception">The exception to be thrown.</param> + /// <param name="message">The message from the caller class.</param> + /// <param name="logger">The logger from the caller class.</param> + public static void Throw(Exception exception, string message, Logger logger) + { + string logMessage = string.Concat(DiagnosticsMessages.ExceptionThrowing, " ", exception.GetType().Name, " ", message); + if (logger == null) + { + Console.WriteLine("Exception caught before logger is initiated, error message: " + logMessage + exception.Message); + } + else + { + logger.Log(Level.Error, logMessage, exception); + } + throw exception; + } + + /// <summary> + /// Call this method to throw an exception. + /// </summary> + /// <remarks> + /// Calling this method will trace the exception and do other common processing, + /// and then it will throw the exception. This method traces the exception type + /// and message at error level and the full stack trace at all other levels. + /// </remarks> + /// <example> + /// Exceptions.Throw(new Exception("Some exception")); + /// </example> + /// <param name="exception">The exception to be thrown.</param> + /// <param name="logger">The logger of the caller class.</param> + public static void Throw(Exception exception, Logger logger) + { + Throw(exception, string.Empty, logger); + } + + /// <summary> + /// Call this method every time when an exception is caught. + /// </summary> + /// <remarks> + /// Calling this method will trace the exception and do other common processing. + /// This method traces the exception type and message at error level and the full + /// stack trace at all other levels. + /// </remarks> + /// <example> + /// try + /// { + /// // Some code that can throw + /// } + /// catch (Exception e) + /// { + /// Exceptions.Caught(e); + /// // Exception handling code + /// } + /// </example> + /// <param name="exception">The exception being caught.</param> + /// <param name="level">The log level.</param> + /// <param name="logger">The logger from the caller class.</param> + public static void Caught(Exception exception, Level level, Logger logger) + { + Caught(exception, level, string.Empty, logger); + } + + /// <summary> + /// Call this method every time when an exception is caught. + /// </summary> + /// <remarks> + /// Calling this method will trace the exception and do other common processing. + /// This method traces the exception type and message at error level and the full + /// stack trace at all other levels. + /// </remarks> + /// <example> + /// try + /// { + /// // Some code that can throw + /// } + /// catch (Exception e) + /// { + /// Exceptions.Caught(e); + /// // Exception handling code + /// } + /// </example> + /// <param name="exception">The exception being caught.</param> + /// <param name="level">The log level.</param> + /// <param name="message">The additional messag to log.</param> + /// <param name="logger">The Logger from the caller class.</param> + public static void Caught(Exception exception, Level level, string message, Logger logger) + { + string logMessage = string.Concat(DiagnosticsMessages.ExceptionCaught, " ", exception.GetType().Name, " ", message); + if (logger == null) + { + Console.WriteLine("Exception caught before logger is initiated, error message: " + logMessage + exception.Message); + } + else + { + logger.Log(level, logMessage, exception); + } + } + + public static void CaughtAndThrow(Exception exception, Level level, Logger logger) + { + CaughtAndThrow(exception, level, string.Empty, logger); + } + + public static void CaughtAndThrow(Exception exception, Level level, string message, Logger logger) + { + string logMessage = string.Concat(DiagnosticsMessages.ExceptionCaught, " ", exception.GetType().Name, " ", message); + if (logger == null) + { + Console.WriteLine("Exception caught before logger is initiated, error message: " + logMessage + exception.Message); + } + else + { + logger.Log(level, logMessage, exception); + } + throw exception; + } + + /// <summary> + /// This method returns true if the exception passed as parameter is a critical exception + /// that should have not been caught. Examples for such exceptions are StackOverflowException + /// and OutOfMemoryException. + /// </summary> + /// <remarks> + /// Catch statements which catch all exceptions must call this method immediately and rethrow + /// wihtout further processing if the method returns true. + /// </remarks> + /// <example> + /// try + /// { + /// // Some code that can throw + /// } + /// catch (Exception e) + /// { + /// if (Exceptions.MustRethrow(e)) + /// { + /// throw; + /// } + /// // Exception handling code + /// } + /// </example> + /// <param name="exception">The exception to be checked.</param> + /// <returns>True if the exceptions is critical one and should not be caught and false otherwise.</returns> + public static bool MustRethrow(Exception exception) + { + return (exception is OutOfMemoryException || + exception is StackOverflowException); + } + + /// <summary> + /// Gets an exception message that includes the messages of the inner exceptions.. + /// </summary> + /// <param name="e">The excption.</param> + /// <returns>The meessage</returns> + public static string GetFullMessage(Exception e) + { + var fullMessage = new StringBuilder(); + bool firstLevel = true; + while (e != null) + { + if (firstLevel) + { + firstLevel = false; + } + else + { + fullMessage.Append("-->"); + } + fullMessage.Append(e.Message); + e = e.InnerException; + } + + return fullMessage.ToString(); + } + + /// <summary> + /// Call this method to throw ArgumentException for an invalid argument. + /// </summary> + /// <param name="argumentName">The invalid argument name.</param> + /// <param name="message">A message explaining the reason for th exception.</param> + /// <param name="logger">The logger of the caller class.</param> + public static void ThrowInvalidArgument(string argumentName, string message, Logger logger) + { + Throw(new ArgumentException(message, argumentName), logger); + } + + /// <summary> + /// Call this method to throw ArgumentOutOfRangeException exception. + /// </summary> + /// <param name="argumentName">The invalid argument name.</param> + /// <param name="message">A message explaining the reason for th exception.</param> + /// <param name="logger">The logger of the caller class.</param> + public static void ThrowArgumentOutOfRange(string argumentName, string message, Logger logger) + { + Throw(new ArgumentOutOfRangeException(argumentName, message), logger); + } + + /// <summary> + /// Call this method to check if an argument is null and throw ArgumentNullException exception. + /// </summary> + /// <param name="argument">The argument to be checked.</param> + /// <param name="name">The name of the argument.</param> + /// <param name="logger">The logger of the caller class.</param> + public static void ThrowIfArgumentNull(object argument, string name, Logger logger) + { + if (argument == null) + { + Exceptions.Throw(new ArgumentNullException(name), logger); + } + } + + /// <summary> + /// Call this method to throw ObjectDisposedException if an object is disposed. + /// </summary> + /// <remarks> + /// All disposable objects should check their state and throw in the beginning of each public method. + /// This helper method provides a shorter way to do this. + /// </remarks> + /// <example> + /// class SomeClass : IDisposable + /// { + /// bool _disposed; + /// // ... + /// public void SomePublicMethod() + /// { + /// Exceptions.ThrowIfObjectDisposed(_disposed, this); + /// // Method's code + /// } + /// } + /// </example> + /// <param name="disposed">True if the object is disposed.</param> + /// <param name="o">The object.</param> + /// <param name="logger">The logger of the caller class.</param> + public static void ThrowIfObjectDisposed(bool disposed, object o, Logger logger) + { + if (disposed) + { + Throw(new ObjectDisposedException(o.GetType().Name), logger); + } + } + #endregion + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Utilities/IIdentifiable.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Utilities/IIdentifiable.cs b/lang/cs/Org.Apache.REEF.Utilities/IIdentifiable.cs new file mode 100644 index 0000000..532349c --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Utilities/IIdentifiable.cs @@ -0,0 +1,26 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +namespace Org.Apache.REEF.Utilities +{ + public interface IIdentifiable + { + string Id { get; set; } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Utilities/IMessage.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Utilities/IMessage.cs b/lang/cs/Org.Apache.REEF.Utilities/IMessage.cs new file mode 100644 index 0000000..97c4f60 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Utilities/IMessage.cs @@ -0,0 +1,33 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +namespace Org.Apache.REEF.Utilities +{ + /// <summary> + /// A message from a REEF component + /// </summary> + public interface IMessage + { + /// <summary> + /// Get Message payload + /// </summary> + /// <returns></returns> + byte[] Message { get; } + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Utilities/Logging/JavaLoggingSetting.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Utilities/Logging/JavaLoggingSetting.cs b/lang/cs/Org.Apache.REEF.Utilities/Logging/JavaLoggingSetting.cs new file mode 100644 index 0000000..3b26c34 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Utilities/Logging/JavaLoggingSetting.cs @@ -0,0 +1,39 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +namespace Org.Apache.REEF.Utilities.Logging +{ + public enum JavaLoggingSetting + { + /// <summary> + /// info level log, and not transferred to CLR + /// </summary> + INFO = 0, + + /// <summary> + /// verbose log, but not to CLR + /// </summary> + VERBOSE = 1, + + /// <summary> + /// verbose log, transferred to CLR + /// </summary> + VERBOSE_TO_CLR = 2, + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Utilities/Logging/Level.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Utilities/Logging/Level.cs b/lang/cs/Org.Apache.REEF.Utilities/Logging/Level.cs new file mode 100644 index 0000000..3c47e59 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Utilities/Logging/Level.cs @@ -0,0 +1,59 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +namespace Org.Apache.REEF.Utilities.Logging +{ + public enum Level + { + /// <summary> + /// Output no tracing and debugging messages. + /// </summary> + Off = 0, + + /// <summary> + /// Output error-handling messages. + /// </summary> + Error = 1, + + /// <summary> + /// Output warnings and error-handling messages. + /// </summary> + Warning = 2, + + /// <summary> + /// Trace a start event + /// </summary> + Start = 3, + + /// <summary> + /// Trace a stop event + /// </summary> + Stop = 4, + + /// <summary> + /// Output informational messages, warnings, and error-handling messages. + /// </summary> + Info = 5, + + /// <summary> + /// Output all debugging and tracing messages. + /// </summary> + Verbose = 6, + } +}
