com.fujitsu.vdmj.in.patterns.INMapPattern Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
*
* Copyright (c) 2016 Fujitsu Services Ltd.
*
* Author: Nick Battle
*
* This file is part of VDMJ.
*
* VDMJ is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VDMJ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VDMJ. If not, see .
* SPDX-License-Identifier: GPL-3.0-or-later
*
******************************************************************************/
package com.fujitsu.vdmj.in.patterns;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Vector;
import com.fujitsu.vdmj.in.patterns.visitors.INPatternVisitor;
import com.fujitsu.vdmj.lex.LexLocation;
import com.fujitsu.vdmj.runtime.Context;
import com.fujitsu.vdmj.runtime.PatternMatchException;
import com.fujitsu.vdmj.runtime.ValueException;
import com.fujitsu.vdmj.util.Selector;
import com.fujitsu.vdmj.util.Utils;
import com.fujitsu.vdmj.values.NameValuePair;
import com.fujitsu.vdmj.values.NameValuePairList;
import com.fujitsu.vdmj.values.NameValuePairMap;
import com.fujitsu.vdmj.values.Value;
import com.fujitsu.vdmj.values.ValueMap;
public class INMapPattern extends INPattern
{
private static final long serialVersionUID = 1L;
public final INMapletPatternList maplets;
public INMapPattern(LexLocation location, INMapletPatternList maplets)
{
super(location);
this.maplets = maplets;
}
@Override
public String toString()
{
if (maplets.isEmpty())
{
return "{|->}";
}
else
{
return Utils.listToString("{", maplets, ", ", "}");
}
}
@Override
public int getLength()
{
return maplets.size();
}
@Override
public List getAllNamedValues(Value expval, Context ctxt)
throws PatternMatchException
{
ValueMap values = null;
try
{
values = expval.mapValue(ctxt);
}
catch (ValueException e)
{
patternFail(e);
}
if (values.size() != maplets.size())
{
patternFail(4152, "Wrong number of elements for map pattern");
}
// Since the member patterns may indicate specific map members, we
// have to permute through the various map orderings to see
// whether there are any which match both sides. If the members
// are not constrained however, the initial ordering will be
// fine.
List