/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file 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 Amazon.DynamoDBv2.Model; using Amazon.Runtime; using Amazon.Runtime.Internal.Util; namespace Amazon.DynamoDBv2.DocumentModel { /// /// A DynamoDBEntry that represents a DynamoDB list (L) type. /// public class DynamoDBList : DynamoDBEntry { private static DynamoDBEntryConversion conversion = CreateConversion(); private static DynamoDBEntryConversion CreateConversion() { var conversion = DynamoDBEntryConversion.V2.Clone(); var supportedMemberTypes = new Type[] { typeof(String), typeof(MemoryStream), typeof(byte[]) }; conversion.AddConverter(new DynamoDBListConverter(supportedMemberTypes)); return conversion; } #region Constructors/static initializers /// /// Constructs an empty DynamoDBList. /// public DynamoDBList() { Entries = new List(); } /// /// Construct an instance DynamoDBList. /// /// public DynamoDBList(IEnumerable entries) : this() { Entries.AddRange(entries); } /// /// Create a DynamODBList from an IEnumerable /// /// /// /// public static DynamoDBList Create(IEnumerable items) { var list = new DynamoDBList(); foreach (var item in items) { var entry = new UnconvertedDynamoDBEntry(item); list.Entries.Add(entry); } return list; } #endregion #region Properties/Accessors /// /// Collection of DynamoDB entries /// public List Entries { get; private set; } /// /// Gets or sets DynamoDB at a specific location in the list. /// /// Index of the DynamoDB in question. /// DynamoDB in question. public DynamoDBEntry this[int i] { get { return Entries[i]; } set { if (i < Entries.Count && i >= 0) { Entries[i] = value; } else { throw new ArgumentOutOfRangeException("i"); } } } /// /// Adds a DynamoDB to the end of the list. /// /// DynamoDB to add. public void Add(DynamoDBEntry value) { this.Entries.Add(value); } /// /// Returns a new instance of Document where all unconverted .NET types /// are converted to DynamoDBEntry types using a specific conversion. /// /// /// public DynamoDBList ForceConversion(DynamoDBEntryConversion conversion) { DynamoDBList newList = new DynamoDBList(); for (int i = 0; i < Entries.Count; i++) { var entry = Entries[i]; var unconvertedEntry = entry as UnconvertedDynamoDBEntry; if (unconvertedEntry != null) entry = unconvertedEntry.Convert(conversion); var doc = entry as Document; if (doc != null) entry = doc.ForceConversion(conversion); var list = entry as DynamoDBList; if (list != null) entry = list.ForceConversion(conversion); newList.Add(entry); } return newList; } #endregion #region Overrides internal override AttributeValue ConvertToAttributeValue(AttributeConversionConfig conversionConfig) { if (Entries == null) return null; AttributeValue attribute = new AttributeValue(); var items = new List(); foreach (var entry in Entries) { AttributeValue entryAttributeValue; using (conversionConfig.CRT.Track(entry)) { entryAttributeValue = entry.ConvertToAttributeValue(conversionConfig); } if(entryAttributeValue != null) { items.Add(entryAttributeValue); } } attribute.L = items; attribute.IsLSet = true; return attribute; } /// /// Implement the Clone method.s /// /// public override object Clone() { DynamoDBList list = new DynamoDBList(); foreach (DynamoDBEntry entry in this.Entries) { list.Add(entry.Clone() as DynamoDBEntry); } return list; } /// /// Implement the Equals method. /// /// /// public override bool Equals(object obj) { var otherList = obj as DynamoDBList; if (otherList == null) return false; var entries = Entries; var otherEntries = otherList.Entries; if (entries.Count != otherEntries.Count) return false; for(int i=0;i /// Implement the GetHashCode method. /// /// public override int GetHashCode() { var entriesHashCode = 0; foreach (var entry in this.Entries) { entriesHashCode = Hashing.CombineHashes(entriesHashCode, entry.GetHashCode()); } return entriesHashCode; } #endregion #region Explicit and Implicit conversions #region DynamoDBEntry-DynamoDBList conversions /// /// Explicitly convert DynamoDBList to DynamoDBEntry[] /// /// DynamoDBEntry[] value of this object public override DynamoDBEntry[] AsArrayOfDynamoDBEntry() { return AsListOfDynamoDBEntry().ToArray(); } /// /// Implicitly convert DynamoDBEntry[] to DynamoDBList /// /// DynamoDBEntry[] data to convert /// DynamoDBList representing the data public static implicit operator DynamoDBList(DynamoDBEntry[] data) { return new DynamoDBList(data); } /// /// Explicitly convert DynamoDBList to DynamoDBEntry[] /// /// DynamoDBList to convert /// DynamoDBEntry[] value of DynamoDBList public static explicit operator DynamoDBEntry[](DynamoDBList p) { return p.AsArrayOfDynamoDBEntry(); } /// /// Explicitly convert DynamoDBList to List<DynamoDBEntry> /// /// List<DynamoDBEntry> value of this object public override List AsListOfDynamoDBEntry() { return Entries; } /// /// Implicitly convert List<DynamoDBEntry> to DynamoDBList /// /// List<DynamoDBEntry> data to convert /// DynamoDBList representing the data public static implicit operator DynamoDBList(List data) { return new DynamoDBList(data); } /// /// Explicitly convert DynamoDBList to List<DynamoDBEntry> /// /// DynamoDBList to convert /// List<DynamoDBEntry> value of DynamoDBList public static explicit operator List(DynamoDBList p) { return p.AsListOfDynamoDBEntry(); } #endregion /// /// Explicitly convert DynamoDBList to List<Document> /// /// List<Document> value of this object public override List AsListOfDocument() { return Entries.Select(e => e.ToDocument()).ToList(); } /// /// Implicitly convert List<Document> to DynamoDBEntry /// /// List<Document> data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBList(List data) { return new DynamoDBList(data.Cast()); } /// /// Explicitly convert DynamoDBEntry to List<Document> /// /// DynamoDBEntry to convert /// List<Document> value of DynamoDBEntry public static explicit operator List(DynamoDBList p) { return p.AsListOfDocument(); } /// /// Explicitly convert DynamoDBEntry to String[] /// /// String[] value of this object public override String[] AsArrayOfString() { return AsListOfString().ToArray(); } /// /// Implicitly convert String[] to DynamoDBEntry /// /// String[] data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBList(String[] data) { return conversion.ConvertToEntry(data).ToDynamoDBList(); } /// /// Explicitly convert DynamoDBEntry to String[] /// /// DynamoDBEntry to convert /// String[] value of DynamoDBEntry public static explicit operator String[](DynamoDBList p) { return p.AsArrayOfString(); } /// /// Explicitly convert DynamoDBList to List<String> /// /// List<String> value of this object public override List AsListOfString() { return conversion.ConvertFromEntry>(this); } /// /// Implicitly convert List<String> to DynamoDBList /// /// List<String> data to convert /// DynamoDBList representing the data public static implicit operator DynamoDBList(List data) { return conversion.ConvertToEntry>(data).ToDynamoDBList(); } /// /// Explicitly convert DynamoDBList to List<String> /// /// DynamoDBList to convert /// List<String> value of DynamoDBList public static explicit operator List(DynamoDBList p) { return p.AsListOfString(); } /// /// Explicitly convert DynamoDBEntry to HashSet<String> /// /// List<String> value of this object public override HashSet AsHashSetOfString() { return conversion.ConvertFromEntry>(this); } /// /// Implicitly convert HashSet<String> to DynamoDBList /// /// HashSet<String> data to convert /// DynamoDBList representing the data public static implicit operator DynamoDBList(HashSet data) { return conversion.ConvertToEntry>(data).ToDynamoDBList(); } /// /// Explicitly convert DynamoDBList to HashSet<String> /// /// DynamoDBList to convert /// HashSet<String> value of DynamoDBList public static explicit operator HashSet(DynamoDBList p) { return p.AsHashSetOfString(); } /// /// Explicitly convert DynamoDBList to byte[] /// /// List<byte[]> value of this object public override List AsListOfByteArray() { return conversion.ConvertFromEntry>(this); } /// /// Implicitly convert List<byte[]> to DynamoDBList /// /// List<byte[]> data to convert /// DynamoDBList representing the data public static implicit operator DynamoDBList(List data) { return conversion.ConvertToEntry>(data).ToDynamoDBList(); } /// /// Explicitly convert DynamoDBList to List<byte[]> /// /// DynamoDBList to convert /// List<byte[]> value of DynamoDBList public static explicit operator List(DynamoDBList p) { return p.AsListOfByteArray(); } /// /// Explicitly convert DynamoDBList to HashSet<byte[]> /// /// HashSet<byte[]> value of this object public override HashSet AsHashSetOfByteArray() { return conversion.ConvertFromEntry>(this); } /// /// Implicitly convert HashSet<byte[]> to DynamoDBList /// /// HashSet<byte[]> data to convert /// DynamoDBList representing the data public static implicit operator DynamoDBList(HashSet data) { return conversion.ConvertToEntry>(data).ToDynamoDBList(); } /// /// Explicitly convert DynamoDBList to HashSet<byte[]> /// /// DynamoDBList to convert /// HashSet<byte[]> value of DynamoDBList public static explicit operator HashSet(DynamoDBList p) { return conversion.ConvertFromEntry>(p); } /// /// Explicitly convert DynamoDBList to List<MemoryStream> /// /// List<MemoryStream> value of this object public override List AsListOfMemoryStream() { return conversion.ConvertFromEntry>(this); } /// /// Implicitly convert List<MemoryStream> to DynamoDBList /// /// List<MemoryStream> data to convert /// DynamoDBList representing the data public static implicit operator DynamoDBList(List data) { return conversion.ConvertToEntry>(data).ToDynamoDBList(); } /// /// Explicitly convert DynamoDBList to List<MemoryStream> /// /// DynamoDBList to convert /// List<MemoryStream> value of DynamoDBList public static explicit operator List(DynamoDBList p) { return p.AsListOfMemoryStream(); } #endregion } }