/
AttributeSource.cs
710 lines (639 loc) · 30.5 KB
/
AttributeSource.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Diagnostics;
using Lucene.Net.Support;
using Lucene.Net.Support.Threading;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using FlagsAttribute = Lucene.Net.Analysis.TokenAttributes.FlagsAttribute;
using JCG = J2N.Collections.Generic;
namespace Lucene.Net.Util
{
/*
* 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.
*/
/// <summary>
/// An <see cref="AttributeSource"/> contains a list of different <see cref="Attribute"/>s,
/// and methods to add and get them. There can only be a single instance
/// of an attribute in the same <see cref="AttributeSource"/> instance. This is ensured
/// by passing in the actual type of the <see cref="IAttribute"/> to
/// the <see cref="AddAttribute{T}"/>, which then checks if an instance of
/// that type is already present. If yes, it returns the instance, otherwise
/// it creates a new instance and returns it.
/// </summary>
public class AttributeSource
{
/// <summary>
/// An <see cref="AttributeFactory"/> creates instances of <see cref="Attribute"/>s.
/// </summary>
public abstract class AttributeFactory // LUCENENET TODO: API - de-nest
{
/// <summary>
/// returns an <see cref="Attribute"/> for the supplied <see cref="IAttribute"/> interface.
/// </summary>
public abstract Attribute CreateAttributeInstance<T>() where T : IAttribute;
/// <summary>
/// This is the default factory that creates <see cref="Attribute"/>s using the
/// <see cref="Type"/> of the supplied <see cref="IAttribute"/> interface by removing the <code>I</code> from the prefix.
/// </summary>
public static readonly AttributeFactory DEFAULT_ATTRIBUTE_FACTORY = new DefaultAttributeFactory();
private sealed class DefaultAttributeFactory : AttributeFactory
{
// LUCENENET: Using ConditionalWeakTable instead of WeakIdentityMap. A Type IS an
// identity for a class, so there is no need for an identity wrapper for it.
private static readonly ConditionalWeakTable<Type, WeakReference<Type>> attClassImplMap =
new ConditionalWeakTable<Type, WeakReference<Type>>();
private static readonly object attClassImplMapLock = new object();
internal DefaultAttributeFactory()
{
}
public override Attribute CreateAttributeInstance<S>()
{
try
{
Type attributeType = GetClassForInterface<S>();
// LUCENENET: Optimize for creating instances of the most common attributes
// directly rather than using Activator.CreateInstance()
return CreateInstance(attributeType) ?? (Attribute)Activator.CreateInstance(attributeType);
}
catch (Exception e) when (e.IsInstantiationException() || e.IsIllegalAccessException())
{
throw new ArgumentException("Could not instantiate implementing class for " + typeof(S).FullName, e);
}
}
// LUCENENET: optimize known creation of built-in types
private Attribute CreateInstance(Type attributeType)
{
if (ReferenceEquals(typeof(CharTermAttribute), attributeType))
return new CharTermAttribute();
if (ReferenceEquals(typeof(FlagsAttribute), attributeType))
return new FlagsAttribute();
if (ReferenceEquals(typeof(OffsetAttribute), attributeType))
return new OffsetAttribute();
if (ReferenceEquals(typeof(PayloadAttribute), attributeType))
return new PayloadAttribute();
if (ReferenceEquals(typeof(PositionIncrementAttribute), attributeType))
return new PositionIncrementAttribute();
if (ReferenceEquals(typeof(PositionLengthAttribute), attributeType))
return new PositionLengthAttribute();
if (ReferenceEquals(typeof(TypeAttribute), attributeType))
return new TypeAttribute();
return null;
}
internal static Type GetClassForInterface<T>() where T : IAttribute
{
var attClass = typeof(T);
Type clazz;
// LUCENENET: If the weakreference is dead, we need to explicitly update its key.
// We synchronize on attClassImplMapLock to make the operation atomic.
UninterruptableMonitor.Enter(attClassImplMapLock);
try
{
if (!attClassImplMap.TryGetValue(attClass, out var @ref) || !@ref.TryGetTarget(out clazz))
{
#if FEATURE_CONDITIONALWEAKTABLE_ADDORUPDATE
attClassImplMap.AddOrUpdate(attClass, CreateAttributeWeakReference(attClass, out clazz));
#else
attClassImplMap.Remove(attClass);
attClassImplMap.Add(attClass, CreateAttributeWeakReference(attClass, out clazz));
#endif
}
}
finally
{
UninterruptableMonitor.Exit(attClassImplMapLock);
}
return clazz;
}
// LUCENENET specific - factored this out so we can reuse
private static WeakReference<Type> CreateAttributeWeakReference(Type attributeInterfaceType, out Type clazz)
{
try
{
string name = ConvertAttributeInterfaceToClassName(attributeInterfaceType);
return new WeakReference<Type>(clazz = attributeInterfaceType.Assembly.GetType(name, true));
}
catch (Exception e)
{
throw new ArgumentException("Could not find implementing class for " + attributeInterfaceType.Name, e);
}
}
private static string ConvertAttributeInterfaceToClassName(Type attributeInterfaceType)
{
int lastPlus = attributeInterfaceType.FullName.LastIndexOf('+');
if (lastPlus == -1)
{
return string.Concat(
attributeInterfaceType.Namespace,
".",
attributeInterfaceType.Name.Substring(1));
}
else
{
return string.Concat(
attributeInterfaceType.FullName.Substring(0, lastPlus + 1),
attributeInterfaceType.Name.Substring(1));
}
}
}
}
/// <summary>
/// This class holds the state of an <see cref="AttributeSource"/>. </summary>
/// <seealso cref="CaptureState()"/>
/// <seealso cref="RestoreState(State)"/>
public sealed class State // LUCENENET specific: Not implementing ICloneable per Microsoft's recommendation
{
internal Attribute attribute;
internal State next;
public object Clone()
{
State clone = new State();
clone.attribute = (Attribute)attribute.Clone();
if (next != null)
{
clone.next = (State)next.Clone();
}
return clone;
}
}
// These two maps must always be in sync!!!
// So they are private, final and read-only from the outside (read-only iterators)
private readonly IDictionary<Type, Util.Attribute> attributes;
private readonly IDictionary<Type, Util.Attribute> attributeImpls;
private readonly State[] currentState;
private readonly AttributeFactory factory;
/// <summary>
/// An <see cref="AttributeSource"/> using the default attribute factory <see cref="AttributeSource.AttributeFactory.DEFAULT_ATTRIBUTE_FACTORY"/>.
/// </summary>
public AttributeSource()
: this(AttributeFactory.DEFAULT_ATTRIBUTE_FACTORY)
{
}
/// <summary>
/// An <see cref="AttributeSource"/> that uses the same attributes as the supplied one.
/// </summary>
public AttributeSource(AttributeSource input)
{
if (input is null)
{
throw new ArgumentNullException(nameof(input), "input AttributeSource must not be null"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentNullException (.NET convention)
}
this.attributes = input.attributes;
this.attributeImpls = input.attributeImpls;
this.currentState = input.currentState;
this.factory = input.factory;
}
/// <summary>
/// An <see cref="AttributeSource"/> using the supplied <see cref="AttributeFactory"/> for creating new <see cref="IAttribute"/> instances.
/// </summary>
public AttributeSource(AttributeFactory factory)
{
this.attributes = new JCG.LinkedDictionary<Type, Util.Attribute>();
this.attributeImpls = new JCG.LinkedDictionary<Type, Util.Attribute>();
this.currentState = new State[1];
this.factory = factory;
}
/// <summary>
/// Returns the used <see cref="AttributeFactory"/>.
/// </summary>
public AttributeFactory GetAttributeFactory()
{
return this.factory;
}
/// <summary>
/// Returns a new iterator that iterates the attribute classes
/// in the same order they were added in.
/// </summary>
public IEnumerator<Type> GetAttributeClassesEnumerator()
{
return attributes.Keys.GetEnumerator();
}
/// <summary>
/// Returns a new iterator that iterates all unique <see cref="IAttribute"/> implementations.
/// This iterator may contain less entries than <see cref="GetAttributeClassesEnumerator()"/>,
/// if one instance implements more than one <see cref="IAttribute"/> interface.
/// </summary>
public IEnumerator<Attribute> GetAttributeImplsEnumerator()
{
State initState = GetCurrentState();
if (initState != null)
{
return new IteratorAnonymousClass(initState);
}
else
{
return Collections.EmptySet<Attribute>().GetEnumerator();
}
}
private class IteratorAnonymousClass : IEnumerator<Attribute>
{
public IteratorAnonymousClass(AttributeSource.State initState)
{
state = initState;
}
private Attribute current;
private State state;
//public virtual void Remove() // LUCENENET specific - not used
//{
// throw UnsupportedOperationException.Create();
//}
public void Dispose()
{
}
public bool MoveNext()
{
if (state == null)
{
return false;
}
Attribute att = state.attribute;
state = state.next;
current = att;
return true;
}
public void Reset()
{
throw UnsupportedOperationException.Create();
}
public Attribute Current => current;
object IEnumerator.Current => current;
}
/// <summary>
/// A cache that stores all interfaces for known implementation classes for performance (slow reflection) </summary>
// LUCENENET: Using ConditionalWeakTable instead of WeakIdentityMap. A Type IS an
// identity for a class, so there is no need for an identity wrapper for it.
private static readonly ConditionalWeakTable<Type, LinkedList<WeakReference<Type>>> knownImplClasses =
new ConditionalWeakTable<Type, LinkedList<WeakReference<Type>>>();
internal static LinkedList<WeakReference<Type>> GetAttributeInterfaces(Type clazz)
{
return knownImplClasses.GetValue(clazz, (key) =>
{
// we have the slight chance that another thread may do the same, but who cares?
LinkedList<WeakReference<Type>> foundInterfaces = new LinkedList<WeakReference<Type>>();
// find all interfaces that this attribute instance implements
// and that extend the Attribute interface
Type actClazz = clazz;
do
{
foreach (Type curInterface in actClazz.GetInterfaces())
{
if (curInterface != typeof(IAttribute) && typeof(IAttribute).IsAssignableFrom(curInterface))
{
foundInterfaces.AddLast(new WeakReference<Type>(curInterface));
}
}
actClazz = actClazz.BaseType;
} while (actClazz != null);
return foundInterfaces;
});
}
/// <summary>
/// <b>Expert:</b> Adds a custom <see cref="Attribute"/> instance with one or more <see cref="IAttribute"/> interfaces.
/// <para><font color="red"><b>Please note:</b> It is not guaranteed, that <paramref name="att"/> is added to
/// the <see cref="AttributeSource"/>, because the provided attributes may already exist.
/// You should always retrieve the wanted attributes using <see cref="GetAttribute{T}"/> after adding
/// with this method and cast to your <see cref="Type"/>.
/// The recommended way to use custom implementations is using an <see cref="AttributeFactory"/>.
/// </font></para>
/// </summary>
public void AddAttributeImpl(Attribute att)
{
Type clazz = att.GetType();
if (attributeImpls.ContainsKey(clazz))
{
return;
}
LinkedList<WeakReference<Type>> foundInterfaces = GetAttributeInterfaces(clazz);
// add all interfaces of this Attribute to the maps
foreach (var curInterfaceRef in foundInterfaces)
{
curInterfaceRef.TryGetTarget(out Type curInterface);
if (Debugging.AssertsEnabled) Debugging.Assert(curInterface != null, "We have a strong reference on the class holding the interfaces, so they should never get evicted");
// Attribute is a superclass of this interface
if (!attributes.ContainsKey(curInterface))
{
// invalidate state to force recomputation in captureState()
this.currentState[0] = null;
attributes.Add(curInterface, att);
if (!attributeImpls.ContainsKey(clazz))
{
attributeImpls.Add(clazz, att);
}
}
}
}
/// <summary>
/// The caller must pass in an interface type that extends <see cref="IAttribute"/>.
/// This method first checks if an instance of the corresponding class is
/// already in this <see cref="AttributeSource"/> and returns it. Otherwise a
/// new instance is created, added to this <see cref="AttributeSource"/> and returned.
/// </summary>
public T AddAttribute<T>()
where T : IAttribute
{
var attClass = typeof(T);
// LUCENENET: Eliminated exception and used TryGetValue
if (!attributes.TryGetValue(attClass, out var result))
{
if (!(attClass.IsInterface && typeof(IAttribute).IsAssignableFrom(attClass)))
{
throw new ArgumentException("AddAttribute() only accepts an interface that extends IAttribute, but " + attClass.FullName + " does not fulfil this contract.");
}
result = this.factory.CreateAttributeInstance<T>();
AddAttributeImpl(result);
}
return (T)(IAttribute)result;
}
/// <summary>
/// Returns <c>true</c>, if this <see cref="AttributeSource"/> has any attributes </summary>
public bool HasAttributes => this.attributes.Count > 0;
/// <summary>
/// The caller must pass in an interface type that extends <see cref="IAttribute"/>.
/// Returns <c>true</c>, if this <see cref="AttributeSource"/> contains the corrsponding <see cref="Attribute"/>.
/// </summary>
public bool HasAttribute<T>() where T : IAttribute
{
var attClass = typeof(T);
return this.attributes.ContainsKey(attClass);
}
/// <summary>
/// The caller must pass in an interface type that extends <see cref="IAttribute"/>.
/// Returns the instance of the corresponding <see cref="Attribute"/> contained in this <see cref="AttributeSource"/>
/// </summary>
/// <exception cref="ArgumentException"> if this <see cref="AttributeSource"/> does not contain the
/// <see cref="Attribute"/>. It is recommended to always use <see cref="AddAttribute{T}()"/> even in consumers
/// of <see cref="Analysis.TokenStream"/>s, because you cannot know if a specific <see cref="Analysis.TokenStream"/> really uses
/// a specific <see cref="Attribute"/>. <see cref="AddAttribute{T}()"/> will automatically make the attribute
/// available. If you want to only use the attribute, if it is available (to optimize
/// consuming), use <see cref="HasAttribute{T}()"/>. </exception>
public virtual T GetAttribute<T>() where T : IAttribute
{
var attClass = typeof(T);
if (!attributes.TryGetValue(attClass, out var result))
{
throw new ArgumentException($"this AttributeSource does not have the attribute '{attClass.Name}'.");
}
return (T)(IAttribute)result;
}
private State GetCurrentState()
{
State s = currentState[0];
if (s != null || !HasAttributes)
{
return s;
}
var c = s = currentState[0] = new State();
using var it = attributeImpls.Values.GetEnumerator();
it.MoveNext();
c.attribute = it.Current;
while (it.MoveNext())
{
c.next = new State();
c = c.next;
c.attribute = it.Current;
}
return s;
}
/// <summary>
/// Resets all <see cref="Attribute"/>s in this <see cref="AttributeSource"/> by calling
/// <see cref="Attribute.Clear()"/> on each <see cref="IAttribute"/> implementation.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ClearAttributes()
{
for (State state = GetCurrentState(); state != null; state = state.next)
{
state.attribute.Clear();
}
}
/// <summary>
/// Captures the state of all <see cref="Attribute"/>s. The return value can be passed to
/// <see cref="RestoreState(State)"/> to restore the state of this or another <see cref="AttributeSource"/>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual State CaptureState()
{
State state = this.GetCurrentState();
return (state == null) ? null : (State)state.Clone();
}
/// <summary>
/// Restores this state by copying the values of all attribute implementations
/// that this state contains into the attributes implementations of the targetStream.
/// The targetStream must contain a corresponding instance for each argument
/// contained in this state (e.g. it is not possible to restore the state of
/// an <see cref="AttributeSource"/> containing a <see cref="Analysis.TokenAttributes.ICharTermAttribute"/> into a <see cref="AttributeSource"/> using
/// a <see cref="Analysis.Token"/> instance as implementation).
/// <para/>
/// Note that this method does not affect attributes of the targetStream
/// that are not contained in this state. In other words, if for example
/// the targetStream contains an <see cref="Analysis.TokenAttributes.IOffsetAttribute"/>, but this state doesn't, then
/// the value of the <see cref="Analysis.TokenAttributes.IOffsetAttribute"/> remains unchanged. It might be desirable to
/// reset its value to the default, in which case the caller should first
/// call <see cref="AttributeSource.ClearAttributes()"/> (<c>TokenStream.ClearAttributes()</c> on the targetStream.
/// </summary>
public void RestoreState(State state)
{
if (state is null)
{
return;
}
do
{
if (!attributeImpls.ContainsKey(state.attribute.GetType()))
{
throw new ArgumentException("State contains Attribute of type " + state.attribute.GetType().Name + " that is not in in this AttributeSource");
}
state.attribute.CopyTo(attributeImpls[state.attribute.GetType()]);
state = state.next;
} while (state != null);
}
public override int GetHashCode()
{
int code = 0;
for (State state = GetCurrentState(); state != null; state = state.next)
{
code = code * 31 + state.attribute.GetHashCode();
}
return code;
}
public override bool Equals(object obj)
{
if (obj == this)
{
return true;
}
if (obj is AttributeSource other)
{
if (HasAttributes)
{
if (!other.HasAttributes)
{
return false;
}
if (this.attributeImpls.Count != other.attributeImpls.Count)
{
return false;
}
// it is only equal if all attribute impls are the same in the same order
State thisState = this.GetCurrentState();
State otherState = other.GetCurrentState();
while (thisState != null && otherState != null)
{
if (otherState.attribute.GetType() != thisState.attribute.GetType() || !otherState.attribute.Equals(thisState.attribute))
{
return false;
}
thisState = thisState.next;
otherState = otherState.next;
}
return true;
}
else
{
return !other.HasAttributes;
}
}
else
{
return false;
}
}
/// <summary>
/// This method returns the current attribute values as a string in the following format
/// by calling the <see cref="ReflectWith(IAttributeReflector)"/> method:
///
/// <list type="bullet">
/// <item><term>if <paramref name="prependAttClass"/>=true:</term> <description> <c>"AttributeClass.Key=value,AttributeClass.Key=value"</c> </description></item>
/// <item><term>if <paramref name="prependAttClass"/>=false:</term> <description> <c>"key=value,key=value"</c> </description></item>
/// </list>
/// </summary>
/// <seealso cref="ReflectWith(IAttributeReflector)"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReflectAsString(bool prependAttClass)
{
StringBuilder buffer = new StringBuilder();
ReflectWith(new AttributeReflectorAnonymousClass(prependAttClass, buffer));
return buffer.ToString();
}
private class AttributeReflectorAnonymousClass : IAttributeReflector
{
private readonly bool prependAttClass;
private readonly StringBuilder buffer;
public AttributeReflectorAnonymousClass(bool prependAttClass, StringBuilder buffer)
{
this.prependAttClass = prependAttClass;
this.buffer = buffer;
}
public void Reflect<T>(string key, object value)
where T : IAttribute
{
Reflect(typeof(T), key, value);
}
public void Reflect(Type attClass, string key, object value)
{
if (buffer.Length > 0)
{
buffer.Append(',');
}
if (prependAttClass)
{
buffer.Append(attClass.Name).Append('#');
}
buffer.Append(key).Append('=');
if (value is null)
buffer.Append("null");
else
buffer.Append(value);
}
}
/// <summary>
/// This method is for introspection of attributes, it should simply
/// add the key/values this <see cref="AttributeSource"/> holds to the given <see cref="IAttributeReflector"/>.
///
/// <para>This method iterates over all <see cref="IAttribute"/> implementations and calls the
/// corresponding <see cref="Attribute.ReflectWith(IAttributeReflector)"/> method.</para>
/// </summary>
/// <seealso cref="Attribute.ReflectWith(IAttributeReflector)"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReflectWith(IAttributeReflector reflector)
{
for (State state = GetCurrentState(); state != null; state = state.next)
{
state.attribute.ReflectWith(reflector);
}
}
/// <summary>
/// Performs a clone of all <see cref="Attribute"/> instances returned in a new
/// <see cref="AttributeSource"/> instance. This method can be used to e.g. create another <see cref="Analysis.TokenStream"/>
/// with exactly the same attributes (using <see cref="AttributeSource(AttributeSource)"/>).
/// You can also use it as a (non-performant) replacement for <see cref="CaptureState()"/>, if you need to look
/// into / modify the captured state.
/// </summary>
public AttributeSource CloneAttributes()
{
AttributeSource clone = new AttributeSource(this.factory);
if (HasAttributes)
{
// first clone the impls
for (State state = GetCurrentState(); state != null; state = state.next)
{
//clone.AttributeImpls[state.attribute.GetType()] = state.attribute.Clone();
var impl = (Attribute)state.attribute.Clone();
if (!clone.attributeImpls.ContainsKey(impl.GetType()))
{
clone.attributeImpls.Add(impl.GetType(), impl);
}
}
// now the interfaces
foreach (var entry in this.attributes)
{
clone.attributes.Add(entry.Key, clone.attributeImpls[entry.Value.GetType()]);
}
}
return clone;
}
/// <summary>
/// Copies the contents of this <see cref="AttributeSource"/> to the given target <see cref="AttributeSource"/>.
/// The given instance has to provide all <see cref="IAttribute"/>s this instance contains.
/// The actual attribute implementations must be identical in both <see cref="AttributeSource"/> instances;
/// ideally both <see cref="AttributeSource"/> instances should use the same <see cref="AttributeFactory"/>.
/// You can use this method as a replacement for <see cref="RestoreState(State)"/>, if you use
/// <see cref="CloneAttributes()"/> instead of <see cref="CaptureState()"/>.
/// </summary>
public void CopyTo(AttributeSource target)
{
for (State state = GetCurrentState(); state != null; state = state.next)
{
Attribute targetImpl = target.attributeImpls[state.attribute.GetType()];
if (targetImpl is null)
{
throw new ArgumentException("this AttributeSource contains Attribute of type " + state.attribute.GetType().Name + " that is not in the target");
}
state.attribute.CopyTo(targetImpl);
}
}
/// <summary>
/// Returns a string consisting of the class's simple name, the hex representation of the identity hash code,
/// and the current reflection of all attributes. </summary>
/// <seealso cref="ReflectAsString(bool)"/>
public override string ToString()
{
return this.GetType().Name + '@' + RuntimeHelpers.GetHashCode(this).ToString("x") + " " + ReflectAsString(false);
}
}
}