Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / Web / UI / WebParts / PersonalizationStateInfoCollection.cs / 1305376 / PersonalizationStateInfoCollection.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Web.UI.WebControls.WebParts {
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Util;
[Serializable]
public sealed class PersonalizationStateInfoCollection : ICollection {
private Dictionary _indices;
private bool _readOnly;
// Tried to use the generic type List instead, but it's readonly
// implementation returns a different type that is not assignable to List.
// That would require to maintain two different fields, one for the readonly
// collection when set and one for the modifiable collection version.
//
// So, the cleaner solution is to use ArrayList, though it might have a
// slightly slower perf because of explicit casting.
private ArrayList _values;
public PersonalizationStateInfoCollection() {
_indices = new Dictionary(KeyComparer.Default);
_values = new ArrayList();
}
public int Count {
get {
return _values.Count;
}
}
public PersonalizationStateInfo this[string path, string username] {
get {
if (path == null) {
throw new ArgumentNullException("path");
}
Key key = new Key(path, username);
int index;
if (!_indices.TryGetValue(key, out index)) {
return null;
}
return (PersonalizationStateInfo) _values[index];
}
}
public PersonalizationStateInfo this[int index] {
get {
return (PersonalizationStateInfo) _values[index];
}
}
public void Add(PersonalizationStateInfo data) {
if (data == null) {
throw new ArgumentNullException("data");
}
Key key;
UserPersonalizationStateInfo userData = data as UserPersonalizationStateInfo;
if (userData != null) {
key = new Key(userData.Path, userData.Username);
}
else {
key = new Key(data.Path, null);
}
// VSWhidbey 376063: avoid key duplicate, we check here first before we add.
if (_indices.ContainsKey(key)) {
if (userData != null) {
throw new ArgumentException(SR.GetString(
SR.PersonalizationStateInfoCollection_CouldNotAddUserStateInfo,
key.Path, key.Username));
}
else {
throw new ArgumentException(SR.GetString(
SR.PersonalizationStateInfoCollection_CouldNotAddSharedStateInfo,
key.Path));
}
}
int pos = _values.Add(data);
try {
_indices.Add(key, pos);
}
catch {
// Roll back the first addition to make the whole addition atomic
_values.RemoveAt(pos);
throw;
}
}
public void Clear() {
_values.Clear();
_indices.Clear();
}
public void CopyTo(PersonalizationStateInfo[] array, int index) {
_values.CopyTo(array, index);
}
public IEnumerator GetEnumerator() {
return _values.GetEnumerator();
}
public bool IsSynchronized {
get {
return false;
}
}
public void Remove(string path, string username) {
if (path == null) {
throw new ArgumentNullException("path");
}
Key key = new Key(path, username);
int ipos;
if (!_indices.TryGetValue(key, out ipos)) {
return;
}
Debug.Assert(ipos >= 0 && ipos < _values.Count);
_indices.Remove(key);
try {
_values.RemoveAt(ipos);
}
catch {
// Roll back the first addition to make the whole addition atomic
_indices.Add(key, ipos);
throw;
}
// Readjust the values' indices by -1 where the indices are greater than the removed index
ArrayList al = new ArrayList();
foreach(KeyValuePair de in _indices) {
if (de.Value > ipos) {
al.Add(de.Key);
}
}
foreach (Key k in al) {
_indices[k] = ((int) _indices[k]) - 1;
}
}
public void SetReadOnly() {
if (_readOnly) {
return;
}
_readOnly = true;
_values = ArrayList.ReadOnly(_values);
}
public object SyncRoot {
get {
return this;
}
}
void ICollection.CopyTo(Array array, int index) {
_values.CopyTo(array, index);
}
[Serializable]
private sealed class Key {
public string Path;
public string Username;
internal Key(string path, string username) {
Debug.Assert(path != null);
Path = path;
Username = username;
}
}
[Serializable]
private sealed class KeyComparer : IEqualityComparer {
internal static readonly IEqualityComparer Default = new KeyComparer();
bool IEqualityComparer.Equals(Key x, Key y) {
return (Compare(x, y) == 0);
}
int IEqualityComparer.GetHashCode(Key key) {
if (key == null) {
return 0;
}
Debug.Assert(key.Path != null);
int pathHashCode = key.Path.ToLowerInvariant().GetHashCode();
int usernameHashCode = 0;
if (key.Username != null) {
usernameHashCode = key.Username.ToLowerInvariant().GetHashCode();
}
return HashCodeCombiner.CombineHashCodes(pathHashCode, usernameHashCode);
}
private int Compare(Key x, Key y) {
if (x == null && y == null) {
return 0;
}
if (x == null) {
return -1;
}
if (y == null) {
return 1;
}
int pathDiff = String.Compare(x.Path, y.Path,
StringComparison.OrdinalIgnoreCase);
if (pathDiff != 0) {
return pathDiff;
}
else {
return String.Compare(x.Username, y.Username,
StringComparison.OrdinalIgnoreCase);
}
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Web.UI.WebControls.WebParts {
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Util;
[Serializable]
public sealed class PersonalizationStateInfoCollection : ICollection {
private Dictionary _indices;
private bool _readOnly;
// Tried to use the generic type List instead, but it's readonly
// implementation returns a different type that is not assignable to List.
// That would require to maintain two different fields, one for the readonly
// collection when set and one for the modifiable collection version.
//
// So, the cleaner solution is to use ArrayList, though it might have a
// slightly slower perf because of explicit casting.
private ArrayList _values;
public PersonalizationStateInfoCollection() {
_indices = new Dictionary(KeyComparer.Default);
_values = new ArrayList();
}
public int Count {
get {
return _values.Count;
}
}
public PersonalizationStateInfo this[string path, string username] {
get {
if (path == null) {
throw new ArgumentNullException("path");
}
Key key = new Key(path, username);
int index;
if (!_indices.TryGetValue(key, out index)) {
return null;
}
return (PersonalizationStateInfo) _values[index];
}
}
public PersonalizationStateInfo this[int index] {
get {
return (PersonalizationStateInfo) _values[index];
}
}
public void Add(PersonalizationStateInfo data) {
if (data == null) {
throw new ArgumentNullException("data");
}
Key key;
UserPersonalizationStateInfo userData = data as UserPersonalizationStateInfo;
if (userData != null) {
key = new Key(userData.Path, userData.Username);
}
else {
key = new Key(data.Path, null);
}
// VSWhidbey 376063: avoid key duplicate, we check here first before we add.
if (_indices.ContainsKey(key)) {
if (userData != null) {
throw new ArgumentException(SR.GetString(
SR.PersonalizationStateInfoCollection_CouldNotAddUserStateInfo,
key.Path, key.Username));
}
else {
throw new ArgumentException(SR.GetString(
SR.PersonalizationStateInfoCollection_CouldNotAddSharedStateInfo,
key.Path));
}
}
int pos = _values.Add(data);
try {
_indices.Add(key, pos);
}
catch {
// Roll back the first addition to make the whole addition atomic
_values.RemoveAt(pos);
throw;
}
}
public void Clear() {
_values.Clear();
_indices.Clear();
}
public void CopyTo(PersonalizationStateInfo[] array, int index) {
_values.CopyTo(array, index);
}
public IEnumerator GetEnumerator() {
return _values.GetEnumerator();
}
public bool IsSynchronized {
get {
return false;
}
}
public void Remove(string path, string username) {
if (path == null) {
throw new ArgumentNullException("path");
}
Key key = new Key(path, username);
int ipos;
if (!_indices.TryGetValue(key, out ipos)) {
return;
}
Debug.Assert(ipos >= 0 && ipos < _values.Count);
_indices.Remove(key);
try {
_values.RemoveAt(ipos);
}
catch {
// Roll back the first addition to make the whole addition atomic
_indices.Add(key, ipos);
throw;
}
// Readjust the values' indices by -1 where the indices are greater than the removed index
ArrayList al = new ArrayList();
foreach(KeyValuePair de in _indices) {
if (de.Value > ipos) {
al.Add(de.Key);
}
}
foreach (Key k in al) {
_indices[k] = ((int) _indices[k]) - 1;
}
}
public void SetReadOnly() {
if (_readOnly) {
return;
}
_readOnly = true;
_values = ArrayList.ReadOnly(_values);
}
public object SyncRoot {
get {
return this;
}
}
void ICollection.CopyTo(Array array, int index) {
_values.CopyTo(array, index);
}
[Serializable]
private sealed class Key {
public string Path;
public string Username;
internal Key(string path, string username) {
Debug.Assert(path != null);
Path = path;
Username = username;
}
}
[Serializable]
private sealed class KeyComparer : IEqualityComparer {
internal static readonly IEqualityComparer Default = new KeyComparer();
bool IEqualityComparer.Equals(Key x, Key y) {
return (Compare(x, y) == 0);
}
int IEqualityComparer.GetHashCode(Key key) {
if (key == null) {
return 0;
}
Debug.Assert(key.Path != null);
int pathHashCode = key.Path.ToLowerInvariant().GetHashCode();
int usernameHashCode = 0;
if (key.Username != null) {
usernameHashCode = key.Username.ToLowerInvariant().GetHashCode();
}
return HashCodeCombiner.CombineHashCodes(pathHashCode, usernameHashCode);
}
private int Compare(Key x, Key y) {
if (x == null && y == null) {
return 0;
}
if (x == null) {
return -1;
}
if (y == null) {
return 1;
}
int pathDiff = String.Compare(x.Path, y.Path,
StringComparison.OrdinalIgnoreCase);
if (pathDiff != 0) {
return pathDiff;
}
else {
return String.Compare(x.Username, y.Username,
StringComparison.OrdinalIgnoreCase);
}
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Win32.cs
- PersonalizationAdministration.cs
- DirectoryGroupQuery.cs
- _NativeSSPI.cs
- XDeferredAxisSource.cs
- ByteStorage.cs
- UncommonField.cs
- HMACSHA512.cs
- RequestValidator.cs
- WebBrowserDocumentCompletedEventHandler.cs
- SmiEventStream.cs
- WeakEventTable.cs
- RadioButtonPopupAdapter.cs
- BoolLiteral.cs
- StylusDownEventArgs.cs
- StringUtil.cs
- BitmapImage.cs
- FrameworkContentElementAutomationPeer.cs
- StateDesigner.LayoutSelectionGlyph.cs
- FlowPosition.cs
- TableLayoutStyle.cs
- SettingsPropertyNotFoundException.cs
- SortDescription.cs
- CultureSpecificStringDictionary.cs
- SecurityPolicySection.cs
- SqlDataSourceView.cs
- SmtpException.cs
- ClientApiGenerator.cs
- BlobPersonalizationState.cs
- Privilege.cs
- LoginStatusDesigner.cs
- XmlMessageFormatter.cs
- XmlSchemaSimpleTypeUnion.cs
- UnsafeNativeMethodsMilCoreApi.cs
- XmlAttributeCollection.cs
- ThicknessKeyFrameCollection.cs
- AccessedThroughPropertyAttribute.cs
- ObjectDataProvider.cs
- ClaimTypeElementCollection.cs
- DataGridViewColumnDesigner.cs
- SiteMapNode.cs
- IntegerValidatorAttribute.cs
- SqlProvider.cs
- SqlUtil.cs
- SetState.cs
- CreateUserWizardStep.cs
- XmlObjectSerializerReadContext.cs
- WorkflowPrinting.cs
- RegionInfo.cs
- XmlSignificantWhitespace.cs
- PropertyValueChangedEvent.cs
- SendMessageContent.cs
- FastPropertyAccessor.cs
- ButtonBaseAutomationPeer.cs
- ScriptingSectionGroup.cs
- CounterCreationDataCollection.cs
- XmlSchemaCollection.cs
- SerTrace.cs
- xdrvalidator.cs
- ComponentEvent.cs
- ColumnResizeAdorner.cs
- EntityClassGenerator.cs
- NominalTypeEliminator.cs
- UnsafePeerToPeerMethods.cs
- WSDualHttpBindingElement.cs
- DecodeHelper.cs
- IndexingContentUnit.cs
- PerformanceCounter.cs
- FormViewUpdateEventArgs.cs
- Propagator.cs
- RSAPKCS1SignatureDeformatter.cs
- VScrollProperties.cs
- EventManager.cs
- CatchBlock.cs
- NamespaceInfo.cs
- MILUtilities.cs
- DetailsViewCommandEventArgs.cs
- DropDownButton.cs
- LayoutUtils.cs
- UserControlParser.cs
- webclient.cs
- RotateTransform3D.cs
- TypeSystem.cs
- CallTemplateAction.cs
- xdrvalidator.cs
- DynamicContractTypeBuilder.cs
- XmlNodeComparer.cs
- ArrayElementGridEntry.cs
- ReadOnlyHierarchicalDataSource.cs
- MatrixKeyFrameCollection.cs
- AttributeConverter.cs
- StructuralCache.cs
- ThrowHelper.cs
- FunctionQuery.cs
- ObjectDisposedException.cs
- XmlAttributes.cs
- Header.cs
- WebPartConnection.cs
- CrossContextChannel.cs
- NullableDoubleMinMaxAggregationOperator.cs