[NUI] TCSACR-226 code change (#1032)
[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     /// <since_tizen> 4 </since_tizen>
26     public abstract class MediaCommand
27     {
28         /// <summary>
29         /// Initializes a new instance of the <see cref="MediaCommand"/> class with the specified <see cref="MediaDatabase"/>.
30         /// </summary>
31         /// <param name="database">The <see cref="MediaDatabase"/> that the commands run on.</param>
32         /// <exception cref="ArgumentNullException"><paramref name="database"/> is null.</exception>
33         /// <exception cref="ObjectDisposedException"><paramref name="database"/> has already been disposed.</exception>
34         /// <since_tizen> 4 </since_tizen>
35         protected MediaCommand(MediaDatabase database)
36         {
37             Database = database ?? throw new ArgumentNullException(nameof(database));
38
39             if (database.IsDisposed)
40             {
41                 throw new ObjectDisposedException(nameof(database));
42             }
43         }
44
45         internal void ValidateDatabase()
46         {
47             Database.ValidateState();
48         }
49
50         /// <summary>
51         /// Gets the <see cref="MediaDatabase"/>.
52         /// </summary>
53         /// <value>The <see cref="MediaDatabase"/> which commands execute on.</value>
54         /// <since_tizen> 4 </since_tizen>
55         public MediaDatabase Database { get; }
56     }
57
58     /// <summary>
59     /// Provides a means of reading results obtained by executing a query.
60     /// </summary>
61     /// <since_tizen> 4 </since_tizen>
62     public interface IMediaDataReader
63     {
64         /// <summary>
65         /// Advances to the next record.
66         /// </summary>
67         /// <returns>true if there are more rows; otherwise false.</returns>
68         /// <since_tizen> 4 </since_tizen>
69         bool Read();
70
71         /// <summary>
72         /// Gets the current record.
73         /// </summary>
74         /// <value>The current record object.</value>
75         /// <since_tizen> 4 </since_tizen>
76         object Current { get; }
77     }
78
79     /// <summary>
80     /// Provides a means of reading results obtained by executing a query.
81     /// </summary>
82     /// <typeparam name="TRecord"></typeparam>
83     /// <since_tizen> 4 </since_tizen>
84     public class MediaDataReader<TRecord> : IMediaDataReader, IDisposable
85     {
86         private readonly IEnumerator<TRecord> _enumerator;
87
88         internal MediaDataReader(IEnumerable<TRecord> items)
89         {
90             _enumerator = items.GetEnumerator();
91         }
92
93         /// <summary>
94         /// Gets the current record.
95         /// </summary>
96         /// <value>The current record if the position is valid; otherwise null.</value>
97         /// <since_tizen> 4 </since_tizen>
98         public TRecord Current
99         {
100             get
101             {
102                 ValidateNotDisposed();
103                 return _enumerator.Current;
104             }
105         }
106
107         /// <summary>
108         /// Advances to the next record.
109         /// </summary>
110         /// <returns>true if there are more rows; otherwise false.</returns>
111         /// <since_tizen> 4 </since_tizen>
112         public bool Read()
113         {
114             ValidateNotDisposed();
115             return _enumerator.MoveNext();
116         }
117
118         object IMediaDataReader.Current => Current;
119
120         #region IDisposable Support
121         private bool _disposed = false;
122
123         /// <summary>
124         /// Disposes of the resources (other than memory) used by the MediaDataReader.
125         /// </summary>
126         /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
127         /// <since_tizen> 4 </since_tizen>
128         protected virtual void Dispose(bool disposing)
129         {
130             if (!_disposed)
131             {
132                 _disposed = true;
133             }
134         }
135
136         /// <summary>
137         /// Releases all resources used by the current instance.
138         /// </summary>
139         /// <since_tizen> 4 </since_tizen>
140         public void Dispose()
141         {
142             Dispose(true);
143         }
144
145         internal void ValidateNotDisposed()
146         {
147             if (_disposed)
148             {
149                 throw new ObjectDisposedException(GetType().Name);
150             }
151         }
152         #endregion
153     }
154 }