hazelcast-client
Version: 
Hazelcast - open source In-Memory Data Grid - client for NodeJS
123 lines • 6.48 kB
JavaScript
;
/*
 * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed 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.
 */
Object.defineProperty(exports, "__esModule", { value: true });
var ClassDefinition_1 = require("./ClassDefinition");
var Util = require("../../Util");
var ClassDefinitionWriter = /** @class */ (function () {
    function ClassDefinitionWriter(portableContext, factoryId, classId, version) {
        this.index = 0;
        this.fieldDefinitions = {};
        this.portableContext = portableContext;
        this.buildingDefinition = new ClassDefinition_1.ClassDefinition(factoryId, classId, version);
    }
    ClassDefinitionWriter.prototype.addFieldByType = function (fieldName, fieldType, factoryId, classId) {
        if (factoryId === void 0) { factoryId = 0; }
        if (classId === void 0) { classId = 0; }
        this.fieldDefinitions[fieldName] = new ClassDefinition_1.FieldDefinition(this.index, fieldName, fieldType, factoryId, classId);
        this.index += 1;
    };
    ClassDefinitionWriter.prototype.writeInt = function (fieldName, value) {
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.INT);
    };
    ClassDefinitionWriter.prototype.writeLong = function (fieldName, long) {
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.LONG);
    };
    ClassDefinitionWriter.prototype.writeUTF = function (fieldName, str) {
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.UTF);
    };
    ClassDefinitionWriter.prototype.writeBoolean = function (fieldName, value) {
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.BOOLEAN);
    };
    ClassDefinitionWriter.prototype.writeByte = function (fieldName, value) {
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.BYTE);
    };
    ClassDefinitionWriter.prototype.writeChar = function (fieldName, char) {
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.CHAR);
    };
    ClassDefinitionWriter.prototype.writeDouble = function (fieldName, double) {
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.DOUBLE);
    };
    ClassDefinitionWriter.prototype.writeFloat = function (fieldName, float) {
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.FLOAT);
    };
    ClassDefinitionWriter.prototype.writeShort = function (fieldName, value) {
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.SHORT);
    };
    ClassDefinitionWriter.prototype.writePortable = function (fieldName, portable) {
        Util.assertNotNull(portable);
        var nestedCD = this.portableContext.lookupOrRegisterClassDefinition(portable);
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.PORTABLE, nestedCD.getFactoryId(), nestedCD.getClassId());
    };
    ClassDefinitionWriter.prototype.writeNullPortable = function (fieldName, factoryId, classId) {
        var version = 0;
        var nestedCD = this.portableContext.lookupClassDefinition(factoryId, classId, version);
        if (nestedCD == null) {
            throw new RangeError('Cannot write null portable without explicitly registering class definition!');
        }
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.PORTABLE, nestedCD.getFactoryId(), nestedCD.getClassId());
    };
    ClassDefinitionWriter.prototype.writeByteArray = function (fieldName, bytes) {
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.BYTE_ARRAY);
    };
    ClassDefinitionWriter.prototype.writeBooleanArray = function (fieldName, booleans) {
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.BOOLEAN_ARRAY);
    };
    ClassDefinitionWriter.prototype.writeCharArray = function (fieldName, chars) {
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.CHAR_ARRAY);
    };
    ClassDefinitionWriter.prototype.writeIntArray = function (fieldName, ints) {
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.INT_ARRAY);
    };
    ClassDefinitionWriter.prototype.writeLongArray = function (fieldName, longs) {
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.LONG_ARRAY);
    };
    ClassDefinitionWriter.prototype.writeDoubleArray = function (fieldName, doubles) {
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.DOUBLE_ARRAY);
    };
    ClassDefinitionWriter.prototype.writeFloatArray = function (fieldName, floats) {
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.FLOAT_ARRAY);
    };
    ClassDefinitionWriter.prototype.writeShortArray = function (fieldName, shorts) {
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.SHORT_ARRAY);
    };
    ClassDefinitionWriter.prototype.writeUTFArray = function (fieldName, val) {
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.UTF_ARRAY);
    };
    ClassDefinitionWriter.prototype.writePortableArray = function (fieldName, portables) {
        Util.assertNotNull(portables);
        if (portables.length === 0) {
            throw new RangeError('Cannot write empty array!');
        }
        var sample = portables[0];
        var nestedCD = this.portableContext.lookupOrRegisterClassDefinition(sample);
        this.addFieldByType(fieldName, ClassDefinition_1.FieldType.PORTABLE_ARRAY, nestedCD.getFactoryId(), nestedCD.getClassId());
    };
    ClassDefinitionWriter.prototype.end = function () {
        for (var field in this.fieldDefinitions) {
            this.buildingDefinition.addFieldDefinition(this.fieldDefinitions[field]);
        }
    };
    ClassDefinitionWriter.prototype.getDefinition = function () {
        return this.buildingDefinition;
    };
    ClassDefinitionWriter.prototype.registerAndGet = function () {
        return this.portableContext.registerClassDefinition(this.buildingDefinition);
    };
    return ClassDefinitionWriter;
}());
exports.ClassDefinitionWriter = ClassDefinitionWriter;
//# sourceMappingURL=ClassDefinitionWriter.js.map