001/*
002 * This file is part of the Tuning Fork Visualization Platform
003 *  (http://sourceforge.net/projects/tuningforkvp)
004 *
005 * Copyright (c) 2005 - 2008 IBM Corporation.
006 * All rights reserved. This program and the accompanying materials
007 * are made available under the terms of the Eclipse Public License v1.0
008 * which accompanies this distribution, and is available at
009 * http://www.eclipse.org/legal/epl-v10.html
010 *
011 * Contributors:
012 *     IBM Corporation - initial API and implementation
013 */
014
015package com.ibm.tuningfork.tracegen.chunk;
016
017import org.vmmagic.pragma.Uninterruptible;
018
019@Uninterruptible
020public class PropertyTableChunk extends Chunk {
021
022    public static final int PROPERTY_TABLE_ID = 1;
023    public static final int PROPERTY_COUNT_OFFSET = Chunk.DATA_OFFSET;
024    public static final int PROPERTY_DATA_OFFSET = PROPERTY_COUNT_OFFSET + 4;
025    private int numberOfProperties = 0;
026
027    public PropertyTableChunk() {
028        super(PROPERTY_TABLE_ID);
029        seek(PROPERTY_DATA_OFFSET);
030    }
031
032    public boolean add(String prop, String val) {
033        int guess = JikesRVMSupport.getStringLength(prop) + JikesRVMSupport.getStringLength(val);
034        if (!hasRoom(guess)) {
035            return false;
036        }
037        int savedPosition = getPosition();
038        if (!addString(prop)) {
039            seek(savedPosition);
040            return false;
041        }
042        if (!addString(val)) {
043            seek(savedPosition);
044            return false;
045        }
046        numberOfProperties++;
047        return true;
048    }
049
050    @Override
051    public void close() {
052        putIntAt(PROPERTY_COUNT_OFFSET, numberOfProperties);
053        numberOfProperties = 0;
054        super.close();
055    }
056
057    public boolean hasData() {
058        return numberOfProperties > 0;
059    }
060
061    public void reset() {
062        resetImpl();
063        numberOfProperties = 0;
064        seek(PROPERTY_DATA_OFFSET);
065    }
066}