// -----------------------------------------------------------------------
//
//
// 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 Lucene.Net.Util
{
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Support;
///
/// TODO: Update summary.
///
///
///
///
/// Java File:
/// lucene/src/java/org/apache/lucene/util/AttributeImpl.java
///
///
///
/// C# File:
/// src/Lucene.Net/Util/AttributeBase.cs
///
///
///
/// C# Tests:
/// test/Lucene.Net.Test/Util/AttributeBaseTest.cs
///
///
///
///
public abstract class AttributeBase : IAttribute, ICloneable, ICloneable
{
///
/// Clears the instance.
///
public abstract void Clear();
///
/// Copies this instance to the specified target.
///
/// The attribute base.
public abstract void CopyTo(AttributeBase attributeBase);
///
/// Reflects as string.
///
/// if set to true [prepend attribute type].
/// a
public string ReflectAsString(bool prependAttributeType = false)
{
var buffer = new StringBuilder();
this.Reflect((type, name, value) =>
{
if (buffer.Length > 0)
buffer.Append(",");
if (prependAttributeType)
buffer.Append(type.Name).Append("#");
buffer
.Append(name)
.Append("=")
.Append(value == null ? "null" : value.ToString());
});
return buffer.ToString();
}
///
/// Reflects the specified reflect action.
///
/// The reflect action.
public virtual void Reflect(Action reflectAction)
{
Type type = this.GetType();
LinkedList> foundInterfaces = AttributeSource.GetAttributeInterfaces(type);
if (foundInterfaces.Count == 0)
return;
if (foundInterfaces.Count > 1)
throw new NotSupportedException(
"{0} implements more than one attribute interface. " +
"The default ReflectWith(IAttributeReflector) implementation " +
"can not handle this.".Inject(type.FullName));
PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
foreach (var prop in properties)
{
reflectAction(type, prop.Name, prop.GetValue(this, null));
}
foreach (var field in fields)
{
reflectAction(type, field.Name, field.GetValue(this));
}
}
///
/// Creates a clone of the object, generally shallow.
///
/// an the clone of the current instance.
object ICloneable.Clone()
{
return this.Clone();
}
///
/// Creates a shallow clone of this instance by default.
///
/// an instance of
public virtual AttributeBase Clone()
{
var obj = (AttributeBase)this.MemberwiseClone();
return obj;
}
}
}