#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Collections;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Encodings.ParameterConfigurationEncoding {
// TODO: control creatable item types ### EDIT: done?
[StorableClass]
public class CheckedValueConfigurationList : CheckedItemList, ICheckedValueConfigurationList {
[Storable]
private int minItemCount = 1;
public int MinItemCount {
get { return minItemCount; }
}
[Storable]
protected IItemSet validValues;
public IItemSet ValidValues {
get { return validValues; }
}
#region Constructors and Cloning
[StorableConstructor]
protected CheckedValueConfigurationList(bool deserializing)
: base(deserializing) {
RegisterEvents();
}
protected CheckedValueConfigurationList(CheckedValueConfigurationList original, Cloner cloner)
: base(original, cloner) {
this.minItemCount = original.MinItemCount;
this.validValues = cloner.Clone(original.validValues);
RegisterEvents();
}
public CheckedValueConfigurationList(IItemSet validValues)
: base() {
this.validValues = validValues;
RegisterEvents();
}
public CheckedValueConfigurationList(IEnumerable collection)
: base(collection) {
validValues = null;
// event wiring not needed
}
public CheckedValueConfigurationList()
: base() {
RegisterEvents();
}
public override IDeepCloneable Clone(Cloner cloner) {
return new CheckedValueConfigurationList(this, cloner);
}
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
RegisterEvents();
}
#endregion
private void RegisterEvents() {
this.ItemsAdded += new CollectionItemsChangedEventHandler>(CheckedValueConfigurationList_ItemsAdded);
}
private void DeregisterEvents() {
this.ItemsAdded -= new CollectionItemsChangedEventHandler>(CheckedValueConfigurationList_ItemsAdded);
}
#region Events
private void CheckedValueConfigurationList_ItemsAdded(object sender, CollectionItemsChangedEventArgs> e) {
foreach (var item in e.Items) {
var matchingItems = this.Where(x => x != item.Value && x.ActualValue.ValueDataType == item.Value.ActualValue.ValueDataType);
if (matchingItems.Any()) {
int maxNumber = matchingItems.Select(x => x.Number).Max();
item.Value.Number = maxNumber + 1;
}
}
}
#endregion
}
}