Release 4.0.0-preview1-00172
[platform/core/csapi/tizenfx.git] / src / Tizen.Content.MediaContent / Tizen.Content.MediaContent / MediaCommand.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Collections.Generic;
19
20 namespace Tizen.Content.MediaContent
21 {
22     /// <summary>
23     /// The <see cref="MediaCommand"/> is a base class for command classes.
24     /// </summary>
25     public abstract class MediaCommand
26     {
27         /// <summary>
28         /// Initializes a new instance of the <see cref="MediaCommand"/> class with the specified <see cref="MediaDatabase"/>.
29         /// </summary>
30         /// <param name="database">The <see cref="MediaDatabase"/> that the commands run on.</param>
31         /// <exception cref="ArgumentNullException"><paramref name="database"/> is null.</exception>
32         /// <exception cref="ObjectDisposedException"><paramref name="database"/> has already been disposed of.</exception>
33         protected MediaCommand(MediaDatabase database)
34         {
35             Database = database ?? throw new ArgumentNullException(nameof(database));
36
37             if (database.IsDisposed)
38             {
39                 throw new ObjectDisposedException(nameof(database));
40             }
41         }
42
43         internal void ValidateDatabase()
44         {
45             Database.ValidateState();
46         }
47
48         /// <summary>
49         /// Gets the <see cref="MediaDatabase"/>.
50         /// </summary>
51         /// <value>The <see cref="MediaDatabase"/> which commands execute on.</value>
52         public MediaDatabase Database { get; }
53     }
54
55     /// <summary>
56     /// Provides a means of reading results obtained by executing a query.
57     /// </summary>
58     public interface IMediaDataReader
59     {
60         /// <summary>
61         /// Advances to the next record.
62         /// </summary>
63         /// <returns>true if there are more rows; otherwise false.</returns>
64         bool Read();
65
66         /// <summary>
67         /// Gets the current record.
68         /// </summary>
69         /// <value>The current record object.</value>
70         object Current { get; }
71     }
72
73     /// <summary>
74     /// Provides a means of reading results obtained by executing a query.
75     /// </summary>
76     /// <typeparam name="TRecord"></typeparam>
77     public class MediaDataReader<TRecord> : IMediaDataReader, IDisposable
78     {
79         private readonly IEnumerator<TRecord> _enumerator;
80
81         internal MediaDataReader(IEnumerable<TRecord> items)
82         {
83             _enumerator = items.GetEnumerator();
84         }
85
86         /// <summary>
87         /// Gets the current record.
88         /// </summary>
89         /// <value>The current record if the position is valid; otherwise null.</value>
90         public TRecord Current
91         {
92             get
93             {
94                 ValidateNotDisposed();
95                 return _enumerator.Current;
96             }
97         }
98
99         /// <summary>
100         /// Advances to the next record.
101         /// </summary>
102         /// <returns>true if there are more rows; otherwise false.</returns>
103         public bool Read()
104         {
105             ValidateNotDisposed();
106             return _enumerator.MoveNext();
107         }
108
109         object IMediaDataReader.Current => Current;
110
111         #region IDisposable Support
112         private bool _disposed = false;
113
114         /// <summary>
115         /// Disposes of the resources (other than memory) used by the MediaDataReader.
116         /// </summary>
117         /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
118         protected virtual void Dispose(bool disposing)
119         {
120             if (!_disposed)
121             {
122                 _disposed = true;
123             }
124         }
125
126         /// <summary>
127         /// Releases all resources used by the current instance.
128         /// </summary>
129         public void Dispose()
130         {
131             Dispose(true);
132         }
133
134         internal void ValidateNotDisposed()
135         {
136             if (_disposed)
137             {
138                 throw new ObjectDisposedException(GetType().Name);
139             }
140         }
141         #endregion
142     }
143 }