001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2011-2012 Pentaho and others
008// All Rights Reserved.
009*/
010package mondrian.rolap.agg;
011
012import mondrian.rolap.CellKey;
013import mondrian.util.Pair;
014
015import java.util.*;
016
017/**
018 * Implementation of a segment body which stores the data of a
019 * sparse segment data set into a dense array of java objects.
020 *
021 * @author LBoudreau
022 */
023class SparseSegmentBody extends AbstractSegmentBody {
024    private static final long serialVersionUID = -6684830985364895836L;
025    final CellKey[] keys;
026    final Object[] data;
027
028    SparseSegmentBody(
029        Map<CellKey, Object> dataToSave,
030        List<Pair<SortedSet<Comparable>, Boolean>> axes)
031    {
032        super(axes);
033
034        this.keys = new CellKey[dataToSave.size()];
035        this.data = new Object[dataToSave.size()];
036        int i = 0;
037        for (Map.Entry<CellKey, Object> entry : dataToSave.entrySet()) {
038            keys[i] = entry.getKey();
039            data[i] = entry.getValue();
040            ++i;
041        }
042    }
043
044    @Override
045    protected int getSize() {
046        return keys.length;
047    }
048
049    @Override
050    protected Object getObject(int i) {
051        throw new UnsupportedOperationException();
052    }
053
054    @Override
055    public Map<CellKey, Object> getValueMap() {
056        final Map<CellKey, Object> map =
057            new HashMap<CellKey, Object>(keys.length * 3 / 2);
058        for (int i = 0; i < keys.length; i++) {
059            map.put(keys[i], data[i]);
060        }
061        return map;
062    }
063}
064
065// End SparseSegmentBody.java