Skip to content

Commit

Permalink
Make the whole data model immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
thiemowmde committed Jan 14, 2016
1 parent 721323a commit 3211118
Show file tree
Hide file tree
Showing 25 changed files with 148 additions and 1,626 deletions.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,56 @@ at the heart of the [Wikibase software](http://wikiba.se/).

## Release notes

### 3.0.0 (dev)

The whole data model is now immutable:
* Removed `Claim.setMainSnak`
* Removed `Claim.setQualifiers`
* Removed `Entity.setFingerprint`
* Removed `Fingerprint.removeAliases`
* Removed `Fingerprint.removeAliasesFor`
* Removed `Fingerprint.removeDescription`
* Removed `Fingerprint.removeDescriptionFor`
* Removed `Fingerprint.removeLabel`
* Removed `Fingerprint.removeLabelFor`
* Removed `Fingerprint.setAliases`
* Removed `Fingerprint.setDescription`
* Removed `Fingerprint.setLabel`
* Removed `Group.addItem`
* Removed `Group.removeItem`
* Made `Group.setItemContainer` private
* Removed `GroupableCollection.addItem`
* Removed `GroupableCollection.removeItem`
* Removed `Item.addSiteLink`
* Removed `Item.addStatement`
* Removed `Item.removeSiteLink`
* Removed `Item.removeStatement`
* Removed `List.addItem`
* Removed `List.removeItem`
* Removed `Map.addItem`
* Removed `Map.removeItem`
* Removed `Map.removeItemByKey`
* Removed `Map.setItem`
* Removed `MultiTerm.setLanguageCode`
* Removed `MultiTerm.setTexts`
* Removed `Property.addStatement`
* Removed `Property.removeStatement`
* Removed `Set.addItem`
* Removed `Set.removeItem`
* Removed `Set.removeItemByKey`
* Removed `Set.setItem`
* Removed `SiteLink.setBadges`
* Removed `SnakList.getValidMoveIndices`
* Removed `SnakList.merge`
* Removed `SnakList.move`
* Removed `SnakList.moveDown`
* Removed `SnakList.moveUp`
* Removed `Statement.setClaim`
* Made `Statement.setRank` private
* Removed `Statement.setReferences`
* Removed `Term.setLanguageCode`
* Removed `Term.setText`

### 2.0.0 (2016-01-12)

#### Breaking changes
Expand Down
40 changes: 12 additions & 28 deletions src/Claim.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@
* @param {wikibase.datamodel.SnakList|null} [qualifiers=new wikibase.datamodel.SnakList()]
* @param {string|null} [guid=null] The Global Unique Identifier of this Claim. Can be null if this
* is a new Claim, not yet stored in the database and associated with some entity.
*
* @throws {Error} if parameter is not a Snak instance.
* @throws {Error} if parameter is not a SnakList instance.
*/
var SELF = wb.datamodel.Claim = function WbDataModelClaim( mainSnak, qualifiers, guid ) {
this.setMainSnak( mainSnak );
this.setQualifiers( qualifiers || new wb.datamodel.SnakList() );
if( !( mainSnak instanceof wb.datamodel.Snak ) ) {
throw new Error( 'Main snak needs to be a Snak instance' );
}
if( qualifiers && !( qualifiers instanceof wb.datamodel.SnakList ) ) {
throw new Error( 'Qualifiers have to be a SnakList object' );
}

this._mainSnak = mainSnak;
this._qualifiers = qualifiers || new wb.datamodel.SnakList();
this._guid = guid || null;
};

Expand Down Expand Up @@ -59,39 +69,13 @@ $.extend( SELF.prototype, {
return this._mainSnak;
},

/**
* Overwrites the current main Snak.
*
* @param {wikibase.datamodel.Snak} mainSnak
*
* @throws {Error} if parameter is not a Snak instance.
*/
setMainSnak: function( mainSnak ) {
if( !( mainSnak instanceof wb.datamodel.Snak ) ) {
throw new Error( 'Main snak needs to be a Snak instance' );
}
this._mainSnak = mainSnak;
},

/**
* @return {wikibase.datamodel.SnakList}
*/
getQualifiers: function() {
return this._qualifiers;
},

/**
* @param {wikibase.datamodel.SnakList} qualifiers
*
* @throws {Error} if parameter is not a SnakList instance.
*/
setQualifiers: function( qualifiers ) {
if( !( qualifiers instanceof wb.datamodel.SnakList ) ) {
throw new Error( 'Qualifiers have to be a SnakList object' );
}
this._qualifiers = qualifiers;
},

/**
* @param {*} claim
* @return {boolean}
Expand Down
7 changes: 0 additions & 7 deletions src/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ $.extend( SELF.prototype, {
return this._fingerprint;
},

/**
* @param {wikibase.datamodel.Fingerprint} fingerprint
*/
setFingerprint: function( fingerprint ) {
this._fingerprint = fingerprint;
},

/**
* Returns what type of Entity this is.
*
Expand Down
96 changes: 0 additions & 96 deletions src/Fingerprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,6 @@ $.extend( SELF.prototype, {
return this._labels.hasItemForKey( languageCode );
},

/**
* @param {string} languageCode
* @param {wikibase.datamodel.Term} term
*/
setLabel: function( languageCode, term ) {
this._labels.setItem( languageCode, term );
},

/**
* @param {string} languageCode
* @param {wikibase.datamodel.Term} label
*/
removeLabel: function( languageCode, label ) {
this._labels.removeItem( languageCode, label );
},

/**
* @param {string} languageCode
*/
removeLabelFor: function( languageCode ) {
this._labels.removeItemByKey( languageCode );
},

/**
* @return {wikibase.datamodel.TermMap}
*/
Expand Down Expand Up @@ -142,29 +119,6 @@ $.extend( SELF.prototype, {
return this._descriptions.hasItemForKey( languageCode );
},

/**
* @param {string} languageCode
* @param {wikibase.datamodel.Term} term
*/
setDescription: function( languageCode, term ) {
this._descriptions.setItem( languageCode, term );
},

/**
* @param {string} languageCode
* @param {wikibase.datamodel.Term} description
*/
removeDescription: function( languageCode, description ) {
this._descriptions.removeItem( languageCode, description );
},

/**
* @param {string} languageCode
*/
removeDescriptionFor: function( languageCode ) {
this._descriptions.removeItemByKey( languageCode );
},

/**
* @return {wikibase.datamodel.MultiTermMap}
*/
Expand Down Expand Up @@ -197,56 +151,6 @@ $.extend( SELF.prototype, {
return this._aliases.hasItemForKey( languageCode );
},

/**
* @param {string|wikibase.datamodel.MultiTermMap} languageCodeOrAliases
* @param {wikibase.datamodel.MultiTerm} [aliases]
*
* @throws {Error} when passing a MultiTerm without a language code.
* @throws {Error} when passing a MultiTermMap with a language code.
* @throws {Error} when neither passing a MultiTerm nor a MultiTermMap object.
*/
setAliases: function( languageCodeOrAliases, aliases ) {
var languageCode;

if( typeof languageCodeOrAliases === 'string' ) {
languageCode = languageCodeOrAliases;
} else {
aliases = languageCodeOrAliases;
}

if( aliases instanceof wb.datamodel.MultiTerm ) {
if( !languageCode ) {
throw new Error( 'Language code the wb.datamodel.MultiTerm object should be set '
+ 'for needs to be specified' );
}
this._aliases.setItem( languageCode, aliases );
} else if( aliases instanceof wb.datamodel.MultiTermMap ) {
if( languageCode ) {
throw new Error( 'Unable to handle language code when setting a '
+ 'wb.datamodel.MultiTermMap' );
}
this._aliases = aliases;
} else {
throw new Error( 'Aliases need to be specified as wb.datamodel.MultiTerm or '
+ 'wb.datamodel.MultiTermMap instance' );
}
},

/**
* @param {string} languageCode
* @param {wikibase.datamodel.MultiTerm} aliases
*/
removeAliases: function( languageCode, aliases ) {
this._aliases.removeItem( languageCode, aliases );
},

/**
* @param {string} languageCode
*/
removeAliasesFor: function( languageCode ) {
this._aliases.removeItemByKey( languageCode );
},

/**
* @return {boolean}
*/
Expand Down
25 changes: 1 addition & 24 deletions src/Group.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ $.extend( SELF.prototype, {

/**
* @param {wikibase.datamodel.GroupableCollection} groupableCollection
*
* @throws {Error} when passed GroupableCollection instance contains an item whose key does not
* match the key registered with the Group instance.
* @private
*/
setItemContainer: function( groupableCollection ) {
if( !( groupableCollection instanceof wb.datamodel.GroupableCollection ) ) {
Expand Down Expand Up @@ -114,29 +114,6 @@ $.extend( SELF.prototype, {
return this._groupableCollection.hasItem( item );
},

/**
* @param {*} item
*
* @throws {Error} when trying to add an item whose key does not match the key registered with
* the Group instance.
*/
addItem: function( item ) {
if( this._groupableCollection.getItemKey( item ) !== this._key ) {
throw new Error(
'Mismatching key: Expected ' + this._key + ', received '
+ this._groupableCollection.getItemKey( item )
);
}
this._groupableCollection.addItem( item );
},

/**
* @param {*} item
*/
removeItem: function( item ) {
this._groupableCollection.removeItem( item );
},

/**
* @return {boolean}
*/
Expand Down
16 changes: 0 additions & 16 deletions src/GroupableCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,6 @@ $.extend( SELF.prototype, {
*/
hasItem: util.abstractMember,

/**
* Adds an item to the collection.
* @abstract
*
* @param {*} item
*/
addItem: util.abstractMember,

/**
* Removes an item from the collection.
* @abstract
*
* @param {*} item
*/
removeItem: util.abstractMember,

/**
* Returns whether the collection contains any items.
* @abstract
Expand Down
28 changes: 0 additions & 28 deletions src/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,41 +62,13 @@ var SELF = wb.datamodel.Item = util.inherit(
return this._siteLinkSet;
},

/**
* @param {wikibase.datamodel.SiteLink} siteLink
*/
addSiteLink: function( siteLink ) {
this._siteLinkSet.setSiteLink( siteLink );
},

/**
* @param {wikibase.datamodel.SiteLink} siteLink
*/
removeSiteLink: function( siteLink ) {
this._siteLinkSet.removeSiteLink( siteLink );
},

/**
* @return {wikibase.datamodel.StatementGroupSet}
*/
getStatements: function() {
return this._statementGroupSet;
},

/**
* @param {wikibase.datamodel.Statement} statement
*/
addStatement: function( statement ) {
this._statementGroupSet.addStatement( statement );
},

/**
* @param {wikibase.datamodel.Statement} statement
*/
removeStatement: function( statement ) {
this._statementGroupSet.removeStatement( statement );
},

/**
* @return {boolean}
*/
Expand Down
28 changes: 0 additions & 28 deletions src/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,6 @@ var SELF = wb.datamodel.List = util.inherit(
return false;
},

/**
* @inheritdoc
*/
addItem: function( item ) {
this._assertIsItem( item );

this._items.push( item );
this.length++;
},

/**
* @inheritdoc
*
* @throws {Error} when trying to remove a claim which is not registered.
*/
removeItem: function( item ) {
this._assertIsItem( item );

for( var i = 0; i < this._items.length; i++ ) {
if( this._items[i].equals( item ) ) {
this._items.splice( i, 1 );
this.length--;
return;
}
}
throw new Error( 'Trying to remove a non-existing claim' );
},

/**
* @inheritdoc
*/
Expand Down
Loading

0 comments on commit 3211118

Please sign in to comment.